【问题标题】:C# Execute command Line error with spaceC#用空格执行命令行错误
【发布时间】:2014-12-14 01:26:48
【问题描述】:

这是我的代码:

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

现在结果:

看起来空间有问题,尽管引用了,但我不明白。

【问题讨论】:

  • 取争论行,把它变成一个字符串,在字符串上设置一个断点并读取它的值。将 startInfo.Arguements 指向该字符串。断点应该告诉你所有你需要知道的。
  • 我已经这样做了,它不适合我,因为一切都是正确的,如果我在 cmd 上复制参数值,那就行了。但它不适用于 process.Start() img4.hostingpics.net/pics/302390Sanstitre.png 如果我直接在控制台中复制它,它的工作...

标签: c# command line execute


【解决方案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 + "\"";

【讨论】:

  • 即使代码更干净也不会改变
  • 错误信息说明了什么?您是否正在尝试启动您的 MIMT.exe?为什么需要 cmd.exe?您可以直接开始您的流程。
猜你喜欢
  • 2019-03-27
  • 2019-07-22
  • 2017-05-22
  • 1970-01-01
  • 2014-10-13
  • 1970-01-01
  • 2015-01-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多