如何向Winform C#程序发送2-3个参数?
例如:我将发送类似于MyProg.exe 10 20 "abc"的内容
在我的程序中,我可以接收这些值
(我不想展示MyProg.exe -它将在后台工作)
提前道谢
发布于 2010-05-20 15:31:27
打开Program.cs,它是应用程序的入口点。main方法是启动应用程序的方法,这是entry方法。
你需要通过chaning来修改它:
允许您发送元素的array的其他内容的static void Main()。
尝试将其更改为:
static void Main(string[] args)并遍历args,看看会得到什么结果。
你可以在这里看到更多的例子和解释:Access Command Line Arguments。
有一些很好的库可以帮助你对这些命令行参数进行parse。
示例
为了给你提供更多的信息,我在Kobi提到的另一种方式上放了一个例子:
class Program
{
static void Main()
{
ParseCommnandLineArguments();
}
static void ParseCommnandLineArguments()
{
var args = Environment.GetCommandLineArgs();
foreach(var arg in args)
Console.WriteLine(arg);
}
}CommandLineArguments.exe -q a -b r
然后,将输出
CommandLineArguments.exe
-q
一个
-b
R
用这种方法也可以得到同样的结果。
class Program
{
static void Main(string[] args)
{
foreach (var arg in args)
Console.WriteLine(arg);
}
}发布于 2010-05-20 15:30:43
为此,有一个
Main(params string[] args)
{
}传递给应用程序的所有参数都在字符串数组args中。你可以从那里阅读它们,并做出相应的反应。
https://stackoverflow.com/questions/2871749
复制相似问题