我有一个接收参数的Windows应用程序,我使用它来设置窗口行为
问题是我需要在某些参数中传递文本,但我的应用程序将其视为多个参数,因此:
"http://www.google.com/" contact 450 300 false "Contact Info" true "Stay Visible" true
实际上拥有11个参数而不是我期望的9个。
如何将"contact info"和"stay visible"合并为一个参数?
using System;
class Test
{
static void Main(string[] args)
{
foreach (string arg in args)
{
Console.WriteLine(arg);
}
}
}
执行:
>test.exe first "second arg" third
first
second arg
third
这是一个控制台应用程序,但在传递给Main方法的内容方面与WinForms没有区别。
MSDN表示,它应该按照你提到的方式工作。
class CommandLine
{
static void Main(string[] args)
{
// The Length property provides the number of array elements
System.Console.WriteLine("parameter count = {0}", args.Length);
for (int i = 0; i < args.Length; i++)
{
System.Console.WriteLine("Arg[{0}] = [{1}]", i, args[i]);
}
}
}
你是如何执行你的应用程序的?
如果你是从另一个应用程序中执行它,那么你可能忘记正确格式化参数字符串了:
String arguments = "First \"Sec ond\" Third Fourth \"Fi fth\""
将有五个参数
String arguments = "First Sec ond Third Fourth Fi fth"
将会有七个。
如果参数在快捷方式目标属性中,则同样适用:
"C:\My Path\MyApplication.exe" "Argument 1" Argument2 Argument3 "Argument 4"
替代
"C:\My Path\MyApplication.exe" Argument 1 Argument2 Argument3 Argument 4