[英]Unable to run nbtstat using System.Diagnostic.Process
I have a working piece of code which executes commands using System.Diagnostic.Process. 我有一段代码,使用System.Diagnostic.Process执行命令。 However, when I try to run nbtstat using the same code it does not return anything (neither is there an exception). 但是,当我尝试使用相同的代码运行nbtstat时,它不会返回任何内容(也没有异常)。 When I run hostname (as an example) it returns the hostname. 当我运行主机名(作为示例)时,它返回主机名。
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();
This command 这个命令
nbtstat -A 10.10.10.5
works perfectly well from the command prompt. 从命令提示符完美地运行。 I am not able to understand the problem, neither find resources on the net which could help. 我无法理解这个问题,也无法在网上找到可能有用的资源。 If anyone can guide me in the right direction please? 如果有人能指导我正确的方向吗?
You should call the nbtstat.exe program directly, there is no need to invoke CMD to call it. 你应该直接调用nbtstat.exe程序,不需要调用CMD来调用它。 So use this line instead; 所以请改用这条线;
System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo(@"c:\windows\sysnative\nbtstat.exe", "-A 10.10.10.5");
I also use Sysnative because of Windows64bit redirection. 由于Windows64bit重定向,我也使用Sysnative。 As exaplained in this post 正如这篇文章中提到的那样
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.