从 C# 应用程序执行 netstat returns "File Not Found" 错误
Executing netstat from C# app returns "File Not Found" error
我正在尝试从我的 C# 代码执行 netstat 命令,但出现“未找到文件”错误。
我必须指定“netstat.exe”在哪里吗?
如果是这样,如果(假设)netstat 和 findstr 在两个不同的文件夹中,我该怎么办?
Process cmd = new Process();
cmd.StartInfo.FileName = "netstat -a | findstr 5840";
cmd.StartInfo.RedirectStandardInput = true;
cmd.StartInfo.RedirectStandardOutput = true;
cmd.StartInfo.CreateNoWindow = true;
cmd.StartInfo.UseShellExecute = false;
try
{
cmd.Start();
}
catch (System.ComponentModel.Win32Exception ex)
{
Console.WriteLine(ex.Message);
}
cmd.StandardInput.Flush();
cmd.StandardInput.Close();
cmd.WaitForExit();
StreamReader reader = cmd.StandardOutput;
string output = reader.ReadToEnd();
Console.WriteLine(output);
这是解决问题的方法:
// create the ProcessStartInfo using "cmd" as the program to be run and "/c " as the parameters.
// /c tells cmd that we want it to execute the command that follows and then exit.
string command = "netstat -a | findstr " + sTCPPort;
ProcessStartInfo procStartInfo = new ProcessStartInfo("cmd", "/c " + command);
// The following commands are needed to redirect the standard output.
// This means that it will be redirected to the Process.StandardOutput StreamReader.
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
// Do not create the black window.
procStartInfo.CreateNoWindow = true;
// Now we create a process, assign its ProcessStartInfo and start it
Process proc = new Process();
proc.StartInfo = procStartInfo;
proc.Start();
// Get the output into a string
result = proc.StandardOutput.ReadToEnd();
没有看到确切的信息,很难知道确切的问题,但看起来您可能需要 cmd.exe 并将您的命令作为参数传递给它。
我正在尝试从我的 C# 代码执行 netstat 命令,但出现“未找到文件”错误。 我必须指定“netstat.exe”在哪里吗?
如果是这样,如果(假设)netstat 和 findstr 在两个不同的文件夹中,我该怎么办?
Process cmd = new Process();
cmd.StartInfo.FileName = "netstat -a | findstr 5840";
cmd.StartInfo.RedirectStandardInput = true;
cmd.StartInfo.RedirectStandardOutput = true;
cmd.StartInfo.CreateNoWindow = true;
cmd.StartInfo.UseShellExecute = false;
try
{
cmd.Start();
}
catch (System.ComponentModel.Win32Exception ex)
{
Console.WriteLine(ex.Message);
}
cmd.StandardInput.Flush();
cmd.StandardInput.Close();
cmd.WaitForExit();
StreamReader reader = cmd.StandardOutput;
string output = reader.ReadToEnd();
Console.WriteLine(output);
这是解决问题的方法:
// create the ProcessStartInfo using "cmd" as the program to be run and "/c " as the parameters.
// /c tells cmd that we want it to execute the command that follows and then exit.
string command = "netstat -a | findstr " + sTCPPort;
ProcessStartInfo procStartInfo = new ProcessStartInfo("cmd", "/c " + command);
// The following commands are needed to redirect the standard output.
// This means that it will be redirected to the Process.StandardOutput StreamReader.
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
// Do not create the black window.
procStartInfo.CreateNoWindow = true;
// Now we create a process, assign its ProcessStartInfo and start it
Process proc = new Process();
proc.StartInfo = procStartInfo;
proc.Start();
// Get the output into a string
result = proc.StandardOutput.ReadToEnd();
没有看到确切的信息,很难知道确切的问题,但看起来您可能需要 cmd.exe 并将您的命令作为参数传递给它。