在C#中更改IP地址,子网掩码,DNS服务器和默认网关

时间:2012-10-05 10:53:00

标签: c# networking dns ip gateway

我对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"

我发现了一些人们在此论坛和其他论坛上发布的脚本示例,但我无法让它们发挥作用。

要问可能会有很多问题,但如果有人可以在这里发布有助于我的内容,我将不胜感激。

我真的很感激!

5 个答案:

答案 0 :(得分:2)

您可以使用WMI(Windows管理工具)来实现此目的

这是一个非常好的教程。

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

答案 1 :(得分:0)

我决定采用另一条路线。我不是使用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

简单而有效。

答案 2 :(得分:0)

只需在narfie非常有用的帖子中添加一个额外的帖子(于2014年9月12日13:01)。

如果要将备用DNS地址与主要DNS地址一起设置,请使用以下命令:

  

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

     

netsh interface ipv4 add dns name =“Local Area Connection”196.14.239.2

答案 3 :(得分:0)

检查此应用。它可以通过单击按钮设置以太网和wifi的IP。它可以设置预定义的静态或dhcp配置

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

答案 4 :(得分:-4)

更改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.