简体   繁体   中英

Unable to run nbtstat using System.Diagnostic.Process

I have a working piece of code which executes commands using System.Diagnostic.Process. However, when I try to run nbtstat using the same code it does not return anything (neither is there an exception). 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. 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. As exaplained in this post

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM