简体   繁体   中英

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.

Use Path.GetDirectoryName() and 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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