Click here to Skip to main content
15,903,201 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone,

I must to execute, using cmd.exe, a command.

The command is : netsh interface ip set address "name" static "ip" "subnet" "gateway"

This is my code:
C#
string IP = txtIP.Text;
           string Subnet = txtSubnet.Text;
           string Gateway = txtGateway.Text;
           string Nome_Maschera = '"' + Maschera_di_Rete.Nome_Maschera.ToString() +'"' ;

           DialogResult risultato = MessageBox.Show("Sicuro di voler attribuire i seguenti indirizzi?", "AVVISO", MessageBoxButtons.OKCancel);
           switch (risultato)
           {
               case DialogResult.OK:
                   {

                       string comando;
                       comando = "netsh interface ip set address " + Nome_Maschera.ToString() + " static " + IP.ToString() + " " + Subnet.ToString() + " " + Gateway.ToString();
                       ProcessStartInfo procStartInfo = new ProcessStartInfo();
                       procStartInfo.RedirectStandardOutput = true;
                       procStartInfo.UseShellExecute = false;
                       procStartInfo.CreateNoWindow = true;
                       procStartInfo.FileName = "runas.exe";
                       procStartInfo.Arguments = "/user:Administrator cmd /K " + comando;

                       Process proc = new Process();
                       proc.StartInfo = procStartInfo;
                       proc.Start();

                       string result = proc.StandardOutput.ReadToEnd();
                       break;
                   }
               case DialogResult.Cancel:
                   {
                       break;
                   }
           }


The output isn't correct, because Visual Studio put automatically the symbol "\" before and after the network's name.
Like this: netsh interface ip set address \"Connessione alla rete locale (LAN) \" static 192.168.50.180 255.255.255.0 192.168.50.254

How can I remove the symbol \?
Posted
Comments
[no name] 17-Oct-14 5:26am    
Are you sure that you don't missinterpret what you see in Debugger? Why should VS add "\"?
nicola_melc 17-Oct-14 5:36am    
I don't now why VS add the symbol "\" before and after the network's name.
But, the command doesn't work. I don't have error at the end, but I have the addresses automatically assigned. The command must to assign it static
[no name] 17-Oct-14 5:55am    
What is the type of "Maschera_di_Rete"?
Why you don't remove the "\" by yourself (only for test to check wheter this is really your problem)?
nicola_melc 17-Oct-14 6:05am    
I have two forms:
In the first I use the command: netsh interface ip show config

I put in a listbox every network's name that I found. With doubleclickevent on the selected item I open the second form where I put manually IP, Subnet and Gateway.
I press a button (Confirm).

In the code I assign at the variable Nome_Maschera the Network's name from the first form. In my case, Maschera_di_Rete is the class and Nome_Maschera is the variable
[no name] 17-Oct-14 6:30am    
Why you don't remove the "\" by yourself (only for test to check wheter this is really your problem)?

And BTW it makes it easier if you answer directly to the comment ;)

1 solution

procStartInfo.Arguments = ("/user:Administrator cmd /K " + comando).replace("\","");
 
Share this answer
 
Comments
nicola_melc 17-Oct-14 5:08am    
It doesn't work.
I already tried with this: procStartInfo.Arguments = ("/user:Administrator cmd /K " + comando.Replace(@"\",""));

The output is:
netsh interface ip set address Connessione alla rete locale (LAN) static ip subnet gateway
MukeshSagar 17-Oct-14 5:55am    
I think first try this command from command line prompt whether it is working fine or nor
nicola_melc 17-Oct-14 6:00am    
I' ve tried from command line this:
netsh interface ip set address "Network's Name" static ip subnet gateway
It works correctly.
[no name] 17-Oct-14 6:43am    
Then Launch netsh directly without the use of cmd.exe
nicola_melc 17-Oct-14 8:25am    
How can I run netsh as administrator in C#?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900