Running dos command from c#
Hi
In my app i am trying to dynamically change the ip using netsh, here is my code.
try
{
Process.Start("netsh interface ip set address local static 192.168.2.251 255.255.255.0 192.168.2.251 1");
}
catch (Exception ex)
{
MessageBox.Show(string.Format("Unable to change ip address to dynamic : " + ex.Message), "Network Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
I can run the netsh ok from the command prompt but from my program i keep getting the error 'The system cannot find the file specified'. Can anyone shed some light on this or suggest another way of getting the desired result.
Thanks.
Re: Running dos command from c#
I think
Code:
Process netshProcess = new Process();
netshProcess.StartInfo.FileName = "netsh";
netshProcess.StartInfo.Arguments = "interface ip set address local static 192.168.2.251 255.255.255.0 192.168.2.251 1";
netshProcess.Start();
Should work.
Have to make sure netsh is in the path for the user context the program is running in, too.
Re: Running dos command from c#
Quote:
Originally Posted by MikeVallotton
I think
Code:
Process netshProcess = new Process();
netshProcess.StartInfo.FileName = "netsh";
netshProcess.StartInfo.Arguments = "interface ip set address local static 192.168.2.251 255.255.255.0 192.168.2.251 1";
netshProcess.Start();
Should work.
Have to make sure netsh is in the path for the user context the program is running in, too.
Thanks for the reply. I did manage to find the solution and it is similar to what you have supplied.