在 C# 中更改 IP 地址、子网掩码、dns 服务器和默认网关
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12744712/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change IP address, subnet mask, dns servers and default gateway in C#
提问by narfie
I am VERY new to C#. I want to create a small console app that sets the following:
我对 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
and another small app that clears that information and sets "Obtain an IP address automatically"and "Obtain DNS server address automatically".
和另一个清除该信息并设置"Obtain an IP address automatically"和的小应用程序"Obtain DNS server address automatically"。
I've found some examples of scripts that people have posted on this forum and other forums, but I can't get them to work.
我找到了一些人们在此论坛和其他论坛上发布的脚本示例,但我无法让它们工作。
It might be a lot to ask, but I would appreciate if someone could post something here that will help me.
可能要问很多,但如果有人能在这里发布一些对我有帮助的东西,我将不胜感激。
I really appreciate it!
对此,我真的非常感激!
采纳答案by narfie
I have decided to go another route with this. Instead of using C#, I'm simply going to set it via a batch file.
我决定走另一条路。我不使用 C#,而是简单地通过批处理文件设置它。
set_static_ip.bat
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
The first line sets the ip and default gateway. The second line sets the primary dns server. I haven't been able to set the alternate dns, but i'm sure i'm just missing something.
第一行设置ip和默认网关。第二行设置主 dns 服务器。我无法设置备用 dns,但我确定我只是遗漏了一些东西。
If you want to change the configuration back to obtain an ip address automatically, then you simply run the code below.
如果您想将配置更改回自动获取 IP 地址,那么您只需运行下面的代码。
set_auto_ip.bat
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
Simple yet effective.
简单而有效。
回答by Prabhu Murthy
You can use WMI(Windows management instrumentation) to achieve this
您可以使用 WMI(Windows 管理工具)来实现这一点
Here is an extremely good tutorial to start with.
这是一个非常好的教程。
http://www.codeproject.com/Articles/5697/Configuring-TCP-IP-Settings-using-WMI-and-C
http://www.codeproject.com/Articles/5697/Configuring-TCP-IP-Settings-using-WMI-and-C
回答by NICOCO
Change TCP/IP settings
更改 TCP/IP 设置
TCP/IP defines the language that your computer uses to communicate with other computers. We recommend using automated Dynamic Host Configuration Protocol (DHCP) to automatically assign Internet Protocol (IP) addresses to the computers on your network, if your network supports it. If you use DHCP, then you don't have to change your settings if you move your computer to another location, and DHCP doesn't require you to manually configure settings such as Domain Name System (DNS) and Windows Internet Name Service (WINS).
TCP/IP 定义了您的计算机用于与其他计算机通信的语言。如果您的网络支持,我们建议使用自动动态主机配置协议 (DHCP) 为网络上的计算机自动分配 Internet 协议 (IP) 地址。如果您使用 DHCP,那么您在将计算机移动到另一个位置时无需更改您的设置,并且 DHCP 不需要您手动配置诸如域名系统 (DNS) 和 Windows Internet 名称服务 (WINS) 之类的设置)。
1.
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.
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.
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.
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.
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.
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.
7.
To change DNS, WINS, and IP settings, click Advanced.
回答by Sheener
Just to add one additional point to narfie's very helpful post (on Sep 12 '14 at 13:01).
只是在 narfie 的非常有用的帖子(2014 年 9 月 12 日 13:01)中再补充一点。
If you want to set the alternate DNS address along with the primary one, use the following:
如果要设置备用 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
netsh interface ip set address name="Local Area Connection" static 192.168.0.10 255.255.255.0 192.168.0.1
netsh 接口 ip 设置 dnsservers“本地连接”静态 192.168.0.3 主
netsh interface ipv4 add dns name="Local Area Connection" 196.14.239.2
回答by Kamran
Check this app. It can set IPs for both ethernet and wifi with single button click. It can set predefined static or dhcp config
检查这个应用程序。它可以通过单击按钮为以太网和 wifi 设置 IP。它可以设置预定义的静态或 dhcp 配置

