我有一段代码,它使用System.Diagnostic.Process执行命令。但是,当我尝试使用相同的代码运行nbtstat时,它不会返回任何内容(也没有异常)。当我运行主机名(作为示例)时,它返回主机名。
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
在命令提示符下运行良好。我无法理解这个问题,也无法在网上找到可能有用的资源。如果有人能指导我正确的方向吗?
答案 0 :(得分:0)
你应该直接调用nbtstat.exe程序,不需要调用CMD来调用它。所以请改用此行;
System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo(@"c:\windows\sysnative\nbtstat.exe", "-A 10.10.10.5");
由于Windows64bit重定向,我也使用Sysnative。正如this post
中提到的那样