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

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

问题描述:

我是非常新的C#。我想创建一个小控制台应用程序,设置了以下内容:清除该信息,并设置"Obtain an IP address automatically""Obtain DNS server address automatically"更改IP地址,子网掩码,dns服务器和默认网关在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 

和另一个小应用程序。

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

这可能有很多要问,但我会很感激,如果有人可以发布一些东西,这将帮助我。

我真的很感激它!

+2

你有什么了吗? – oberfreak

我已经决定去与该另一条路线。我只是想通过批处理文件来设置它,而不是使用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 

简单而有效的。

您可以使用WMI(Windows管理规范)来实现这一

这是一个非常好的教程开始。

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

更改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. 
To change DNS, WINS, and IP settings, click Advanced. 

只是添加一个额外的点narfie的非常有用的职位(14年9月12日在13:01)。

如果你想与主之一沿着设置备用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