繁体   English   中英

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

[英]C# Execute command Line error with space

这是我的代码:

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();

现在的结果是:

截图

看起来空间是个问题,尽管有报价,我还是不明白。

不要使用Substring()或类似方法来解析路径和文件名。

使用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 + "\"";

更新:

您可以直接运行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