【问题标题】:Change IP address, subnet mask, dns servers and default gateway in C#在 C# 中更改 IP 地址、子网掩码、dns 服务器和默认网关
【发布时间】:2012-09-26 12:27:19
【问题描述】:

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

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

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

我真的很感激!

【问题讨论】:

    标签: c# networking dns ip gateway


    【解决方案1】:

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

    这是一个非常好的入门教程。

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

    【讨论】:

      【解决方案2】:

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

      简单而有效。

      【讨论】:

        【解决方案3】:

        在 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

        【讨论】:

          【解决方案4】:

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

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

          【讨论】:

            【解决方案5】:

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

            【讨论】:

              猜你喜欢
              • 2017-02-13
              • 1970-01-01
              • 2012-01-22
              • 1970-01-01
              • 2015-10-18
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多