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

How do I use command line arguments in my C# console app?(如何在我的 C# 控制台应用程序中使用命令行参数?)

本文介绍了如何在我的 C# 控制台应用程序中使用命令行参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 url 缩短器应用程序,我还想使用 C# 创建一个控制台应用程序,以将 URL 推送到我也创建的 WCF 服务.

I am writing a url shortener app and I would like to also create a console app with C# to push the URLs to a WCF service which I have also created.

WCF 应用程序将缩短此 URI 上的 url;

WCF app will shorten the url on this URI;

http://example.com/shorten/http://exaple.com

所以我想要的就是这样.

so what I want is just that.

我的控制台 exe 文件将位于 c:dev 文件夹和 Windows 命令行中,我想这样做;

My console exe file will be sitting inside c:dev folder and on Windows command line, I would like to do this;

c:dev>myapp -throw http://example.com

c:dev>myapp -throw http://example.com

我想通过这种方法与该服务交谈.谈话部分没有问题.但问题是我如何在命令行上提供这个 -throw 东西并获得响应并将该响应放在命令行上并提供一种方法将其复制到剪贴板.我在这里问太多了吗?:S 我不知道.

with this method I would like to talk to that service. there is no problem on talking part. But the problem is how can I supply this -throw thing on the command line and get a response and put that response on the command line and supply a method to copy that to the clipboard. Am I asking too much here? :S I don't know.

您能否指导我到某个地方我可以找到相关信息,或者您能给我一个示例代码吗?

Could you direct me somewhere that I can find information on that or could u please give me an example code of this?

谢谢.

我试过下面的代码;

    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:AppsArgsTryArgsTryinDebug>ArgsTry- 扔人

C:AppsArgsTryArgsTryinDebug>ArgsTry -throw man

未处理的异常:System.Threading.ThreadStateException:当前线程必须设置为单线程OLE 之前的线程单元 (STA) 模式可以拨打电话.确保您的主函数具有 STAThreadAttribute标在上面.在System.Windows.Forms.Clipboard.SetDataObject(对象数据,布尔副本,在 t32 retryTimes 中,Int32 重试延迟)在System.Windows.Forms.Clipboard.SetDataObject(对象数据)在ArgsTry.Program.Main(String[] args) 在c:appsArgsTryArgsTryProgram.cs:第 14 行

Unhandled Exception: System.Threading.ThreadStateException: Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensur e that your Main function has STAThreadAttribute marked on it. at System.Windows.Forms.Clipboard.SetDataObject(Object data, Boolean copy, In t32 retryTimes, Int32 retryDelay) at System.Windows.Forms.Clipboard.SetDataObject(Object data) at ArgsTry.Program.Main(String[] args) in c:appsArgsTryArgsTryProgram.cs: line 14

C:AppsArgsTryArgsTryinDebug>

C:AppsArgsTryArgsTryinDebug>

推荐答案

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

Passing arguments to a console application is easy:

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

这篇关于如何在我的 C# 控制台应用程序中使用命令行参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何在我的 C# 控制台应用程序中使用命令行参数?

基础教程推荐