且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在没有窗口的情况下从C#运行命令行并获取输出

更新时间:2023-09-09 22:59:34

此代码为我提供了正确的输出。

This code gives me the correct ouput.

const string ipAddress = "127.0.0.1";
Process process = new Process
{
    StartInfo =
    {
        UseShellExecute = false,
        RedirectStandardOutput = true,
        RedirectStandardError = true,
        CreateNoWindow = true,
        FileName = "cmd.exe",
        Arguments = "/C tracert -d " + ipAddress
    }
};
process.Start();
process.WaitForExit();
if(process.HasExited)
{
    string output = process.StandardOutput.ReadToEnd();
}