【发布时间】:2013-11-26 17:38:14
【问题描述】:
我已经通过 Google 进行了搜索,在这里找到了很多示例,但我似乎无法让我的 Windows 窗体应用程序运行并从命令行获取参数。我真的希望可以选择在没有控制台版本的情况下安排应用程序。但每次我设置 cmd 行参数时,都会出现 CLR20r3 错误。
static void Main(string[] args)
{
if(args != null && args.Length > 0)
{
/*
* arg[1] = Backup Location *require
* arg[2] = Log File - Enable if present *optional
* arg[3] = Debug Messages - Enabled if present *optional
* arg[4] = Backup Type - Type to perform *required
*/
}
else
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.Automatic);
Application.Run(new Form1());
}
}
任何时候我尝试传递一个 arg 它都会出错
myapp.exe "C:\Backup\" => CLR20r3
【问题讨论】:
-
这真的是你所有的代码吗?根据您发布的内容,我没有看到您可以传递参数的任何地方。
-
您收到什么异常?您可能可以在事件查看器中找到它。
-
@Brian 不是我所有的代码只是它的开始,以确保我做对了
-
您是否尝试过调试应用程序?您可以告诉您的 IDE 传递命令行参数。您还可以在尝试解析之前将 args 的内容转储到日志文件中,以确认应用正在接收您认为应该接收的内容。
-
System.NullReferenceException因为 Main 中的 args 不适用于 Windows 窗体应用程序,您应该使用string[] args = Environment.GetCommandLineArgs()
标签: c# winforms command-line-arguments