运行命令cmd通过

时间:2018-02-20 11:59:27

标签: c# .net cmd

如何在c#

中运行此命令
  

telalertc -i bilal -m"测试消息"

string command = "telalertc -i bilal -m  "Test";

System.Diagnostics.ProcessStartInfo procStartInfo =
  new System.Diagnostics.ProcessStartInfo("cmd", " /c " + command);

procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = true;
procStartInfo.Domain = "*.*.*.*";

System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();

string result = proc.StandardOutput.ReadToEnd();

proc.WaitForExit();

Console.WriteLine(result);

如何使用控制台应用程序 C#

运行此命令

1 个答案:

答案 0 :(得分:2)

如果您想开始telalertc启动它,而不是cmd

  System.Diagnostics.ProcessStartInfo procStartInfo = new ProcessStartInfo() {
    FileName = "telalertc",
    Arguments = " -i bilal -m \"Test Message\"",
    RedirectStandardOutput = true,
    UseShellExecute = false,
    CreateNoWindow = true,
    Domain = "*.*.*.*" // do you really want this?
  };

  // Wrap IDisposable into using
  using (var proc = System.Diagnostics.Process.Start(procStartInfo)) {
    // First wait for completeness  
    proc.WaitForExit();
    // Then read results
    string result = proc.StandardOutput.ReadToEnd();
    Console.WriteLine(result);
  }