简体   繁体   English

C#执行带有空格的命令行错误

[英]C# Execute command Line error with space

Here is my code: 这是我的代码:

ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "cmd.exe";
String mypath = Assembly.GetExecutingAssembly().Location.ToString();
mypath = mypath.Substring(0, mypath.LastIndexOf("\\"));
startInfo.Arguments = "/k "+
    string.Format("\"{0}\"" + " " + ProcessIds[clientlist.SelectedIndex] + " " + "\"{1}\"",
                  mypath + "\\MIMT.exe",
                  mypath + "\\No.Ankama.dll");
process.StartInfo = startInfo;
process.Start();

And now the result: 现在的结果是:

截图

Looks like the space is a problem, despite the quote, I do not understand. 看起来空间是个问题,尽管有报价,我还是不明白。

Don't parse the path and file name with Substring() or similar methods. 不要使用Substring()或类似方法来解析路径和文件名。

Use Path.GetDirectoryName() and Path.Combine() . 使用Path.GetDirectoryName()Path.Combine()

string mypath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
string exeFile = Path.Combine(mypath, "MIMT.exe");
string dllFile = Path.Combine(mypath, "No.Ankama.dll");
startInfo.Arguments = "/k \""+ exeFile + "\" " + ProcessIds[clientlist.SelectedIndex] 
    + " \"" + dllFile + "\"";

UPDATE: 更新:

You can run your exe file directly without using the cmd.exe. 您可以直接运行exe文件,而无需使用cmd.exe。

startInfo.FileName = exeFile;
startInfo.Arguments = ProcessIds[clientlist.SelectedIndex] + " \"" + dllFile + "\"";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM