在命令行中传递参数数组

huM*_*pty 6 .net c# .net-4.0 winforms c#-4.0

我看过这个关于在C#中传递命令行参数的问题.

但在我的情况下,我必须将参数数组传递给调用.exe文件.

例如

var arr = new string[] {"Item title","New task","22","High Priority"}
Run Code Online (Sandbox Code Playgroud)

是否可以Process.Start()与exe路径一起使用数组

我有.exe路径

const string path = @"C:\Projects\Test\test.exe";
Run Code Online (Sandbox Code Playgroud)

谢谢

pet*_*kyy -6

请尝试这个:

        var arr = new string[] {"Item title", "New task", "22", "High Priority"};
        const string path = @"C:\Projects\Test\test.exe";
        const string argsSeparator = " ";
        string args = string.Join(argsSeparator, arr);

        Process.Start(path, args);
Run Code Online (Sandbox Code Playgroud)

  • 他如何知道空格是参数还是分隔符的一部分?:D (12认同)
  • 如果“正确”完成并适用于_任意_参数,则会有点[复杂](http://blogs.msdn.com/b/oldnewthing/archive/2010/09/17/10063629.aspx)。基本上,您必须应用 [CommandLineToArgv](http://msdn.microsoft.com/en-us/library/windows/desktop/bb776391(v=vs.85).aspx) 的规则,但相反。另请参阅[此答案](http://stackoverflow.com/a/2611075/21567)。 (9认同)
  • @GrzegorzWilczura 那么也许 `string args = string.Join(" ", arr.Select(e => "\"" + e + "\""));` ? (2认同)
  • @GrzegorzWilczura 如果分隔符不是空格,他可以使用任何其他分隔符:)只需更改 const 即可。感谢您的评论。 (2认同)