C#执行命令行空间错误

时间:2014-12-14 01:26:48

标签: c# command line execute

这是我的代码:

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

现在的结果是:

screenshot

看起来空间是一个问题,尽管引用,我不明白。

1 个答案:

答案 0 :(得分:1)

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