我如何使用命令行参数在我的c#控制台应用程序
本文关键字:我的 控制台 应用程序 参数 何使用 命令行 | 更新日期: 2023-09-27 17:50:31
我正在写一个url缩短应用程序,我还想用c#创建一个控制台应用程序,将url推送到我也创建的WCF服务。
WCF应用程序将缩短此URI上的url;
http://example.com/shorten/http://exaple.com
所以我想要的就是。
我的控制台exe文件将位于c:'dev
文件夹和Windows命令行中,我想这样做;
c:'dev>myapp -throw http://example.com
使用此方法,我想与该服务对话。谈话部分没有问题。但问题是我如何在命令行上提供这个-throw东西并获得响应并将响应放在命令行上并提供一个方法将其复制到剪贴板。我是不是要求太多了?我不知道。
你能指导我的地方,我可以找到有关的信息,或者你能给我这个例子代码吗?
谢谢。编辑:
我试过下面的代码;
class Program {
static void Main(string[] args) {
if (args[0] == "-throw") {
System.Windows.Forms.Clipboard.SetDataObject(args[1]);
Console.WriteLine(args[1] + " has been added to clipboard !");
Console.ReadLine();
}
}
}
和我收到以下错误;
C: ' Apps ' ArgsTry ' ArgsTry ' bin '调试> ArgsTry把人
未处理的例外:System.Threading.ThreadStateException:当前线程必须设置为单线程线程公寓(STA)模式在OLE之前可以打电话。确保你的Main函数有STAThreadAttribute标记在上面。在System.Windows.Forms.Clipboard.SetDataObject(对象数据,布尔复制,在t32的retryTimes,Int32 retryDelay) atSystem.Windows.Forms.Clipboard.SetDataObject(对象数据)ArgsTry.Program。Main(String[] args) inc: ' apps ' ArgsTry ' ArgsTry ' Program.cs:第14行
C: ' Apps ' ArgsTry ' ArgsTry ' bin '调试>
向控制台应用程序传递参数很容易:
using System;
public class CommandLine
{
public static void Main(string[] args)
{
for(int i = 0; i < args.Length; i++)
{
if( args[i] == "-throw" )
{
// call http client args[i+1] for URL
}
}
}
}
关于剪贴板,请参见:
http://msdn.microsoft.com/en-us/library/system.windows.forms.clipboard.aspx查看下面的参数,您可以使用它来读取运行exe文件时传递的所有值。
static void Main(string[] args) {