如何在C#控制台应用程序中使用命令行参数?

tug*_*erk 8 c# console console-application

我正在编写一个url shortener应用程序,我还想用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的东西并获得响应并将该响应放在命令行上并提供一种方法将其复制到剪贴板.我在这里问得太多了吗?:SI不知道.

你能指点一下我能找到相关信息吗?或者你可以给我一个示例代码吗?

谢谢.

编辑: 我尝试了以下代码;

    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();

        }

    }
}
Run Code Online (Sandbox Code Playgroud)

我收到了以下错误;

C:\ Apps\ArgsTry\ArgsTry\bin\Debug> ArgsTry -throw man

未处理的异常:System.Threading.ThreadStateException:在进行OLE调用之前,必须将当前线程设置为单线程单元(STA)模式.确保您的Main函数标记了STAThreadAttribute.在System.Windows.Forms.Clipboard.SetDataObject(对象数据,布尔副本,在t32 retryTimes,Int32 retryDelay中)位于ArgsTry.Program.Main(String [] args)的System.Windows.Forms.Clipboard.SetDataObject(Object data)在c:\ apps\ArgsTry\ArgsTry\Program.cs:第14行

C:\ APPS\ArgsTry\ArgsTry\BIN \调试>

Bla*_*zes 22

将参数传递给控制台应用程序很简单:

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
           }
       }
   }
}
Run Code Online (Sandbox Code Playgroud)

至于剪贴板,请参阅:

http://msdn.microsoft.com/en-us/library/system.windows.forms.clipboard.aspx