开发者论坛

 找回密码
 注册 (请使用非IE浏览器)
查看: 1945|回复: 0

[讨论] C#程序修改IP地址哪里出错了

[复制链接]

0

精华

0

贡献

0

赞扬

帖子
1
软币
57
在线时间
0 小时
注册时间
2017-8-18
发表于 2017-8-18 19:23:53 | 显示全部楼层 |阅读模式
程序运行时并没有报错,可就是修改没反应,哪位指点一下
static void Main(string[] args)
        {
            ManagementClass wmi = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection moc = wmi.GetInstances();
            ManagementBaseObject inPar = null;
            ManagementBaseObject outPar = null;
            string[] ip = { "192.168.1.3", "192.168.1.4" };
            string[] submask = { "255.255.255.0"};
            string[] gatway = { "192.168.1.2", "192.168.1.1" };
            string[] dns = { "114.114.114.114", "114.114.114.114" };
            foreach (ManagementObject mo in moc)
            {
                //如果没有启用IP设置的网络设备则跳过
                if (!(bool)mo["IPEnabled"])
                {
                    continue;
                }
                //设置IP地址和掩码
                if (ip != null && submask != null)
                {
                    inPar = mo.GetMethodParameters("EnableStatic");
                    inPar["IPAddress"] = ip;
                    inPar["SubnetMask"] = submask;
                    outPar = mo.InvokeMethod("EnableStatic", inPar, null);
                }
                //设置网关地址
                if (gatway != null)
                {
                    inPar = mo.GetMethodParameters("SetGateways");
                    inPar["DefaultIPGateway"] = gatway;
                    outPar = mo.InvokeMethod("SetGateways", inPar, null);
                }
                //设置DNS地址
                if (dns != null)
                {
                    inPar = mo.GetMethodParameters("SetDNSServerSearchOrder");
                    inPar["DNSServerSearchOrder"] = dns;
                    outPar = mo.InvokeMethod("SetDNSServerSearchOrder", inPar, null);
                }
            }
        }

回复

使用道具 举报

Archiver|手机版|小黑屋|开发者网 ( 苏ICP备08004430号-2 )
版权所有:南京韵文教育信息咨询有限公司

GMT+8, 2024-3-29 19:26

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表