簡體   English   中英

如何設置網絡適配器的IP地址?

[英]How to set IP Address of Network adapters?

我必須經常更改IP地址才能玩LAN游戲以及在家中使用互聯網。 我正在C#中創建一個可以快速完成的應用程序。 我做了一些字段,例如適配器名稱,IP地址,子網,DNS服務器地址。 我的代碼在設置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;

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

例:

string subnet = comboSubnet.Text;

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

MessageBox.Show(formatted);

格式化該字符串以使其看起來像您想要的任何內容。

您可以使用以下功能獲取當前的適配器配置:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM