I'm trying execute a couple of "netsh" commands but no matter what I do I keep getting the same error "the following command was not found netsh".
I can verify that the path "C:\Windows\System32\netsh.exe" is valid and when I run the same command with the same set of arguments through the command prompt; everything works fine.
This a sample of the code I'm using.
ProcessStartInfo procInfo = new ProcessStartInfo { WorkingDirectory = System.IO.Path.GetPathRoot(System.Environment.SystemDirectory), FileName = @"netsh.exe", RedirectStandardOutput = true, RedirectStandardError = true, UseShellExecute = false, CreateNoWindow = true, Arguments = String.Format(@"{0}\{1} {2}", System.Environment.SystemDirectory, @"netsh.exe", "wlan start hostednetwork"), WindowStyle = ProcessWindowStyle.Hidden }; Process proc = Process.Start(procInfo); proc.WaitForExit(); 1 Answer
You're currently passing the full pathname in the arguments, when I suspect you want to pass it as the filename. For example:
FileName = Path.Combine(Environment.SystemDirectory, "netsh.exe"),
Arguments = "wlan start hostednetwork"For example, this short but complete program doesn't throw any exceptions - I don't know whether it does what you want, but it doesn't fail with the exception you described:
using System;
using System.Diagnostics;
using System.IO;
class Test
{ static void Main() { ProcessStartInfo procInfo = new ProcessStartInfo { WorkingDirectory = Path.GetPathRoot(Environment.SystemDirectory), FileName = Path.Combine(Environment.SystemDirectory, "netsh.exe"), Arguments = "wlan start hostednetwork", RedirectStandardOutput = true, RedirectStandardError = true, UseShellExecute = false, CreateNoWindow = true, WindowStyle = ProcessWindowStyle.Hidden }; Process proc = Process.Start(procInfo); proc.WaitForExit(); }
}(Note how the code becomes less cluttered when you take advantage of using directives instead of using fully-qualified names, by the way.)