如何在我的C#控制台应用程序中使用命令行参数? [英] How do I use command line arguments in my C# console app?
问题描述
我正在写一个url shortener应用程序,我也想创建一个控制台应用程序与C#推送到我还创建的WCF服务的URL。
WCF应用程式会缩短此URI上的网址;
http://example.com/shorten/http://exaple.com
所以我想要的就是这样。
我的控制台exe文件将位于 c:\dev
文件夹和在Windows命令行,我想这样做;
c:\dev> myapp -throw < a href =http://example.com =nofollow> http://example.com
用这种方法我想谈谈的服务。谈话部分没有问题。但问题是如何在命令行上提供 -throw 的东西,并得到响应并将该响应放在命令行上,并提供一个方法将其复制到剪贴板。我在这里要求太多吗? :S我不知道。
您可以指示我在哪里找到相关信息,还是请给我一个示例代码?
感谢。
EDIT:
我试过下面的代码:
类程序{
static void Main(string [] args){
if(args [0] ==-throw){
System.Windows.Forms.Clipboard.SetDataObject(args [1]);
Console.WriteLine(args [1] +已添加到剪贴板!);
Console.ReadLine();
}
}
}
我收到以下错误:
C:\Apps\ArgsTry\ArgsTry\bin\Debug> ArgsTry
-throw man
未处理的异常:
System.Threading.ThreadStateException:
当前线程必须设置为单个
线程公寓(STA)模式,然后可以进行OLE
调用。确保您的
Main函数具有标记的STAThreadAttribute
。
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:\apps\ArgsTry\ArgsTry\Program.cs:
line 14
C:\Apps\ArgsTry\ArgsTry\bin\Debug>
使用系统;
public class CommandLine
{
public static void Main(string [] args)
{
for(int i = 0; i
if(args [i] ==-throw)
{
//调用http客户端args [i + 1] b $ b}
}
}
}
剪贴板,请参阅:
http://msdn.microsoft.com/en-us/library/system.windows.forms.clipboard.aspx
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 app will shorten the url on this URI;
http://example.com/shorten/http://exaple.com
so what I want is just that.
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
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?
Thanks.
EDIT : I have tried the following code;
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();
}
}
}
and I received the following error;
C:\Apps\ArgsTry\ArgsTry\bin\Debug>ArgsTry -throw man
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:\apps\ArgsTry\ArgsTry\Program.cs: line 14
C:\Apps\ArgsTry\ArgsTry\bin\Debug>
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
}
}
}
}
As for the clipboard, see:
http://msdn.microsoft.com/en-us/library/system.windows.forms.clipboard.aspx
这篇关于如何在我的C#控制台应用程序中使用命令行参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!