CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 1999
    Posts
    23

    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.

  2. #2
    Join Date
    Mar 2008
    Posts
    72

    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.

  3. #3
    Join Date
    Jun 1999
    Posts
    23

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured