新的cmd.exe进程

时间:2018-01-04 21:11:01

标签: c#

我正在尝试调用此cmdline应用程序,由于某种原因它永远不会给我任何反馈。当我从cmd运行它时,我得到这样的输出。不确定为什么它没有给我反馈

Version Information
CLI:        7.2.3.3327 11/14/2016 7:12:47 PM (Patch 4)
Agent:      7.2.3.3327 11/14/2016 7:12:47 PM (Patch 4)
Kernel:     7.2.3.3327 11/14/2016 7:12:47 PM (Patch 4)
Server:     7.2.3.3327 11/14/2016 7:12:47 PM (Patch 4)

代码

ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
startInfo.FileName = @"cmd.exe";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.Arguments = @"/c ""C:\Program Files(x86)\Bit9\Parity Agent\dascli.exe"" " + "status";

try
{
    // Start the process with the info we specified.
    // Call WaitForExit and then the using statement will close.
    using (Process exeProcess = Process.Start(startInfo))
    {
        output = exeProcess.StandardOutput.ReadToEnd();
        exeProcess.WaitForExit();
    }
}
catch
{
    // Log error.
}

2 个答案:

答案 0 :(得分:0)

可以将你的try语句修改为

try
{
// Start the process with the info we specified.
// Call WaitForExit and then the using statement will close.
using (Process exeProcess = Process.Start(startInfo))
{
    output = exeProcess.StandardOutput.ReadToEnd();
    exeProcess.WaitForExit();
}
}
catch(Exception e)
{
  Console.WriteLine(e.Message);
}

我怀疑有未被发现的错误。

答案 1 :(得分:0)

cmd处理命令中的引号,如果字符串以1开头,则删除第一个和最后一个引号。运行它的最佳方法是在字符串周围添加引号:

startInfo.Arguments = String.Format("/c \"{0}\"", @"""C:\Program Files (x86)\Bit9\Parity Agent\dascli.exe"" status");

在您的示例中,实际命令行为C:\Program Files(x86)\Bit9\Parity Agent\dascli.exe status,当然在尝试启动C:\Program时失败。

但是,由于您不使用任何命令行处理,因此不需要cmd

startInfo.FileName = @"C:\Program Files (x86)\Bit9\Parity Agent\dascli.exe";
startInfo.Arguments = "status";

这清楚地说明了应该运行的程序是什么,以及它的参数是什么。我还将Program Files(x86)更改为Program Files (x86),因为这是目录的标准名称。