| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 686 人关注过本帖, 2 人收藏
标题:怎样用C#写一个改变本机IP的小程序
只看楼主 加入收藏
candy110
Rank: 1
等 级:新手上路
帖 子:146
专家分:0
注 册:2008-9-25
结帖率:100%
收藏(2)
 问题点数:0 回复次数:2 
怎样用C#写一个改变本机IP的小程序
哪位大虾能指点一下,用C#如何写个变更本机IP的程序谢谢
搜索更多相关主题的帖子: 如何 
2008-09-25 12:49
bygg
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:乖乖的心中
等 级:版主
威 望:241
帖 子:13555
专家分:3076
注 册:2006-10-23
收藏
得分:0 
/// <summary>
        /// 修改IP等信息
        /// </summary>
        void SetNetworkAdapter()
        {
            ManagementBaseObject inPar = null;
            ManagementBaseObject outPar = null;
            ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection moc = mc.GetInstances();
            foreach (ManagementObject mo in moc)
            {
                if (!(bool)mo["IPEnabled"])
                    continue;

                //设置ip地址和子网掩码
                inPar = mo.GetMethodParameters("EnableStatic");
                inPar["IPAddress"] = new string[] { "10.22.21.111", "192.168.10.9" };
                inPar["SubnetMask"] = new string[] { "255.255.255.0", "255.255.255.0" };
                outPar = mo.InvokeMethod("EnableStatic", inPar, null);

                //设置网关地址
                inPar = mo.GetMethodParameters("SetGateways");
                inPar["DefaultIPGateway"] = new string[] { "10.22.21.1", "192.168.10.1" };
                outPar = mo.InvokeMethod("SetGateways", inPar, null);

                //设置DNS
                inPar = mo.GetMethodParameters("SetDNSServerSearchOrder");
                inPar["DNSServerSearchOrder"] = new string[] { "179.32.42.4", "179.32.42.5" };
                outPar = mo.InvokeMethod("SetDNSServerSearchOrder", inPar, null);
                break;
            }
        }

        /**/
        /**/
        /**/
        /// <summary>
        /// 显示当前的ip等信息
        /// </summary>
        void ShowNetworkAdapterInfo()
        {
            Console.WriteLine("****** Current Network Adapter Information ******");
            ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection moc = mc.GetInstances();
            foreach (ManagementObject mo in moc)
            {
                if (!(bool)mo["IPEnabled"])
                    continue;

                Console.WriteLine("{0}\n SVC: '{1}' MAC: [{2}]", (string)mo["Caption"],
                (string)mo["ServiceName"], (string)mo["MACAddress"]);

                string[] addresses = (string[])mo["IPAddress"];
                string[] subnets = (string[])mo["IPSubnet"];
                string[] gateways = (string[])mo["DefaultIPGateway"];
                string[] dnses = (string[])mo["DNSServerSearchOrder"];

                Console.WriteLine(" Addresses :");
                foreach (string sad in addresses)
                    Console.WriteLine("\t'{0}'", sad);

                Console.WriteLine(" Subnets :");
                foreach (string sub in subnets)
                    Console.WriteLine("\t'{0}'", sub);
                Console.WriteLine(" Gateways:");
                foreach (string gw in gateways)
                    Console.WriteLine("\t'{0}'", gw);

                Console.WriteLine(" DNS:");
                foreach (string dns in dnses)
                    Console.WriteLine("\t'{0}'", dns);
            }

        }
====================================================================
    这是从别人那里转来的,你可以看一下...

飘过~~
2008-09-25 13:03
candy110
Rank: 1
等 级:新手上路
帖 子:146
专家分:0
注 册:2008-9-25
收藏
得分:0 
好的,谢谢版主
2008-09-25 13:38
快速回复:怎样用C#写一个改变本机IP的小程序
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019775 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved