簡體   English   中英

在C#中更改IP地址,子網掩碼,DNS服務器和默認網關

[英]Change IP address, subnet mask, dns servers and default gateway in C#

我是C#的新手。 我想創建一個設置以下內容的小型控制台應用程序:

IP address: 192.168.10.133
Subnet mask: 255.255.255.0
Default gateway: 192.168.10.66
Preferred DNS server: 192.168.10.3
Alternate DNS server: 192.168.10.5

另一個小應用程序,清除該信息並設置"Obtain an IP address automatically""Obtain DNS server address automatically" "Obtain an IP address automatically" "Obtain DNS server address automatically"

我找到了一些人們在此論壇和其他論壇上發布的腳本示例,但我無法使它們正常工作。

這可能要問很多,但是如果有人可以在這里發表對我有幫助的東西,我將不勝感激。

我真的很感激!

您可以使用WMI(Windows管理規范)來實現此目的

這是一個非常好的入門教程。

http://www.codeproject.com/Articles/5697/Configuring-TCP-IP-Settings-using-WMI-and-C

我已經決定走這條路。 而不是使用C#,我只是通過批處理文件進行設置。

set_static_ip.bat

netsh interface ip set address name="Local Area Connection" static 192.168.0.10 255.255.255.0 192.168.0.1
netsh interface ip set dnsservers "Local Area Connection" static 192.168.0.3 primary

第一行設置ip和默認網關。 第二行設置主DNS服務器。 我無法設置備用dns,但我確定我只是缺少一些東西。

如果要改回配置以自動獲取IP地址,則只需運行以下代碼。

set_auto_ip.bat

netsh interface ip set address name="Local Area Connection" source="dhcp"
netsh interface ip set dnsservers name="Local Area Connection" source=dhcp

簡單而有效。

只是為narfie的非常有用的帖子(在2014年9月12日13:01)增加了一點。

如果要與主DNS一起設置備用DNS地址,請使用以下命令:

netsh接口ip設置地址名稱=“本地連接”靜態192.168.0.10 255.255.255.0 192.168.0.1

netsh接口ip設置dnsservers“本地連接”靜態192.168.0.3主

netsh接口ipv4添加dns名稱=“本地連接” 196.14.239.2

檢查這個程序。 只需單擊一下按鈕,即可為以太網和wifi設置IP。 它可以設置預定義的靜態或dhcp配置

https://github.com/kamran7679/ConfigureIP.git

更改TCP / IP設置

TCP / IP定義您的計算機用於與其他計算機進行通信的語言。 如果網絡支持,我們建議使用自動動態主機配置協議(DHCP)自動將Internet協議(IP)地址分配給網絡上的計算機。 如果使用DHCP,則無需將計算機移動到其他位置即可更改設置,並且DHCP不需要手動配置諸如域名系統(DNS)和Windows Internet名稱服務(WINS)之類的設置。 )。

1。

  Open Network Connections by clicking the Start button Picture of the Start button, clicking Control Panel, clicking Network and Internet, clicking Network and Sharing Center, and then clicking Manage network connections.

2。

  Right-click the connection that you want to change, and then click Properties. Administrator permission required If you are prompted for an administrator password or confirmation, type the password or provide confirmation.

3。

  Click the Networking tab. Under This connection uses the following items, click either Internet Protocol Version 4 (TCP/IPv4) or Internet Protocol Version 6 (TCP/IPv6), and then click Properties.

4。

  To specify IPv4 IP address settings, do one of the following:
      *

        To obtain IP settings automatically, click Obtain an IP address automatically, and then click OK.
      *

        To specify an IP address, click Use the following IP address, and then, in the IP address, Subnet mask, and Default gateway boxes, type the IP address settings.

5,

  To specify IPv6 IP address settings, do one of the following:
      *

        To obtain IP settings automatically, click Obtain an IPv6 address automatically, and then click OK.
      *

        To specify an IP address, click Use the following IPv6 address, and then, in the IPv6 address, Subnet prefix length, and Default gateway boxes, type the IP address settings.

6。

  To specify DNS server address settings, do one of the following:
      *

        To obtain a DNS server address automatically, click Obtain DNS server address automatically, and then click OK.
      *

        To specify a DNS server address, click Use the following DNS server addresses, and then, in the Preferred DNS server and Alternate DNS server boxes, type the addresses of the primary and secondary DNS servers.

7。

  To change DNS, WINS, and IP settings, click Advanced.

暫無
暫無

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

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