0

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

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

问的可能很多,但如果有人可以在这里发布一些对我有帮助的东西,我将不胜感激。

对此,我真的非常感激!

4

5 回答 5

2

您可以使用 WMI(Windows 管理工具)来实现这一点

这是一个非常好的教程。

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

于 2012-10-05T11:00:44.197 回答
1

我决定用这个走另一条路。我不使用 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

简单而有效。

于 2014-09-12T13:01:05.817 回答
0

只是为 narfie 非常有帮助的帖子添加一点(2014 年 9 月 12 日 13:01)。

如果要设置备用 DNS 地址和主地址,请使用以下命令:

netsh interface ip set address name="本地连接" 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="本地连接" 196.14.239.2

于 2015-06-15T03:28:04.413 回答
0

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

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

于 2017-01-31T08:53:02.947 回答
-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.
于 2012-10-25T21:05:41.870 回答