2013-05-25 303 views
0

我必须非常频繁地更改IP地址才能玩局域网游戏以及在家中使用互联网。我正在C#中创建一个可以快速完成的应用程序。我制作了诸如适配器名称,IP地址,子网,DNS服务器地址等字段。 我它运行在设定的IP按一下按钮代码如下:如何设置网络适配器的IP地址?

string adapter = comboAdapterName.Text; 
string ip = comboIPAddress.Text; 
string subnet = comboSubnet.Text; 
string dns = comboDNS.Text; 

现在我想用这个方法处理从这些领域中获取数据,并相应地追加字符串。

Process p = new Process(); 
ProcessStartInfo psi = new ProcessStartInfo("netsh", "interface ip set address \"Local Area Connection\" static 192.168.0.10 255.255.255.0 192.168.0.1 1"); 
p.StartInfo = psi; 
p.Start(); 

但我想这不是那么容易。因为我无法在不打扰格式的情况下编辑它。此外,我试图创建一个使用多种+一整新的字符串,我可以把这样的:

ProcessStartInfo psi = new ProcessStartInfo(mystring); 

但它毕竟是对我来说太难了。请建议一个简单的方法来做到这一点。

============================================== ============================

我想我明白了:

string ipstring = "netsh interface ip set address " + "\"" + adapter + "\"" + " " + "static" + " " + ip + " " + subnet + " " + dns; 

回答

0

您将需要使用String.Format方法。

例子:

string subnet = comboSubnet.Text; 

string formatted = string.Format("Subnet is: {0}", subnet); 

MessageBox.Show(formatted); 

格式化该字符串看起来像任何你想要的。

+0

以什么方式?海报正在要求对字符串进行协调,并说这对他“太难了”。他不能从他已有的部分建立他的弦乐。 String.Format是从你已有的部分建立一个字符串的方法。 – nvoigt

+0

其实我无法添加“在我的字符串中,多么愚蠢,谢谢你的时间。 –

0

你可以用下面的函数当前适配器配置:

private static void EthernetInf(out string ip, out string dns, out string nic) // To get current ethernet config 
{ 
    ip = ""; 
    dns = ""; 
    nic = ""; 
    foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces()) 
    { 
     if (ni.NetworkInterfaceType == NetworkInterfaceType.Ethernet) 
     { 

      foreach (IPAddress dnsAdress in ni.GetIPProperties().DnsAddresses) 
      { 
       if (dnsAdress.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) 
       { 
        dns = dnsAdress.ToString(); 
       } 
      } 



      foreach (UnicastIPAddressInformation ips in ni.GetIPProperties().UnicastAddresses) 
      { 
       if (ips.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork && !ips.Address.ToString().StartsWith("169")) //to exclude automatic ips 
       { 
        ip = ips.Address.ToString(); 
        nic = ni.Name; 
       } 
      } 
     } 
    } 

下面的函数是用来设置IP在提升的命令提示符:

private void SetIP(Button sender, string arg) //To set IP with elevated cmd prompt 
{ 
    try 
    { 
     if (sender.Background == Brushes.Cyan) 
     { 
      MessageBox.Show("Already Selected..."); 
      return; 
     } 
     ProcessStartInfo psi = new ProcessStartInfo("cmd.exe"); 
     psi.UseShellExecute = true; 
     psi.WindowStyle = ProcessWindowStyle.Hidden; 
     psi.Verb = "runas"; 
     psi.Arguments = arg; 
     Process.Start(psi); 
     if (sender == EthStatic || sender == EthDHCP) 
     { 
      EthStatic.ClearValue(Button.BackgroundProperty); 
      EthDHCP.ClearValue(Button.BackgroundProperty); 

      sender.Background = Brushes.Cyan; 
     } 

     if (sender == WIFIStatic || sender == WIFIDhcp) 
     { 
      WIFIStatic.ClearValue(Button.BackgroundProperty); 
      WIFIDhcp.ClearValue(Button.BackgroundProperty); 

      sender.Background = Brushes.Cyan; 
     } 

    } 
    catch(Exception ex) 
    { 
     MessageBox.Show(ex.Message); 

    } 

此点击按钮的代码传递参数processstartinfo设置IP

private void EthStatic_Click(object sender, RoutedEventArgs e) 
{ 

    SetIP(EthStatic, "/c netsh interface ip set address \"" + EthName + "\" static " + Properties.Settings.Default.EthIPac + " " + Properties.Settings.Default.Subnet + " " + Properties.Settings.Default.EthDnsac + " & netsh interface ip set dns \"" + EthName + "\" static " + Properties.Settings.Default.EthDnsac); 


} 

完整的应用程序可在:

https://github.com/kamran7679/ConfigureIP

相关问题