无法使用 System.Diagnostic.Process 运行 nbtstat
Unable to run nbtstat using System.Diagnostic.Process
我有一段代码可以使用 System.Diagnostic.Process 执行命令。但是,当我尝试使用相同的代码 运行 nbtstat 时,它不会 return 任何东西(也没有例外)。当我 运行 主机名(作为示例)时,它 return 是主机名。
string result = "";
//string commandToExec = "hostname";
string commandToExec = "nbtstat -A 10.10.10.5";
System.Diagnostics.ProcessStartInfo procStartInfo =
new System.Diagnostics.ProcessStartInfo("C:\Windows\System32\cmd.exe", "/c " + commandToExec);
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = true;
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();
result = proc.StandardOutput.ReadToEnd();
这个命令
nbtstat -A 10.10.10.5
在命令提示符下运行良好。我无法理解这个问题,也无法在网上找到可以提供帮助的资源。如果有人可以指导我正确的方向吗?
您应该直接调用nbtstat.exe程序,不需要调用CMD来调用它。所以改用这一行;
System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo(@"c:\windows\sysnative\nbtstat.exe", "-A 10.10.10.5");
由于 Windows64 位重定向,我也使用 Sysnative。如 this post
中所述
我有一段代码可以使用 System.Diagnostic.Process 执行命令。但是,当我尝试使用相同的代码 运行 nbtstat 时,它不会 return 任何东西(也没有例外)。当我 运行 主机名(作为示例)时,它 return 是主机名。
string result = "";
//string commandToExec = "hostname";
string commandToExec = "nbtstat -A 10.10.10.5";
System.Diagnostics.ProcessStartInfo procStartInfo =
new System.Diagnostics.ProcessStartInfo("C:\Windows\System32\cmd.exe", "/c " + commandToExec);
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = true;
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();
result = proc.StandardOutput.ReadToEnd();
这个命令
nbtstat -A 10.10.10.5
在命令提示符下运行良好。我无法理解这个问题,也无法在网上找到可以提供帮助的资源。如果有人可以指导我正确的方向吗?
您应该直接调用nbtstat.exe程序,不需要调用CMD来调用它。所以改用这一行;
System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo(@"c:\windows\sysnative\nbtstat.exe", "-A 10.10.10.5");
由于 Windows64 位重定向,我也使用 Sysnative。如 this post
中所述