【发布时间】:2016-06-17 14:01:07
【问题描述】:
我有一个 winform 应用程序,我将参数“1-2-c:\temp-test”放入项目属性的调试选项卡下的命令行参数文本框中。阅读代码如下。
String[] arguments = Environment.GetCommandLineArgs();
string exeFilename = arguments[0];
string temp = arguments[1]; // return the argument string value. e.g."1-2-c:\temp-test"
string[] args = temp.Split('-');
try
{
string first = "", second="", third="", fourth = "";
if (args != null && args.Count() > 0)
{
first = string.IsNullOrEmpty(args[0]) ? "1" : args[0];
second = string.IsNullOrEmpty(args[1]) ? "2" : args[1];
third = string.IsNullOrEmpty(args[2]) ? @"c:\temp\" : args[2];
fourth = string.IsNullOrEmpty(args[3]) ? "test" : args[3];
}
catch(Exception ex)
{
string argsTest = "";
if (!string.IsNullOrEmpty(args[0]))
argsTest = args[0];
if (!string.IsNullOrEmpty(args[1]))
argsTest = argsTest + " "+ args[1];
if (!string.IsNullOrEmpty(args[2]))
argsTest = argsTest + " " + args[2];
if (!string.IsNullOrEmpty(args[3]))
argsTest = argsTest + " " + args[3];
MessageBox.Show(argsTest + " \n Error: " + ex.ToString());
}
当我从 Visual Studio 运行应用程序时,它工作正常,但是当我转到 bin\debug 并双击 .exe 文件时,我得到了 IndexOutOfRangeException 异常。
另一个观察,如果我使用命令提示符的参数执行这个 exe,它可以正常工作。
双击我的 exe 文件时引发异常。
我搜索并花时间解决,但没有得到任何解决方案。我完全被困住了。任何类型的提示/代码/建议都是可观的。
【问题讨论】:
-
我会推荐NDesk.Options,以免给您带来很多麻烦。