• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

【转载】C#实现动态分配IP和释放IP(DHCP)

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
最近在做一个东西,要用到动态分配IP和释放IP。在C++下很容易就可以实现。但是由于因为网络IP设置是要涉及到硬件,C#是没有现成接口调用的.只能通过调用API或者是WMI这道系统提供给我们的桥梁.主要是通过"Win32_NetworkAdapterConfiguration"这个管理类。

下面附上我写的一个Demo,希望对需要的朋友有帮助,更希望高手来拍砖。

using System;
using System.Collections.Generic;
using System.Text;
using System.Management;
using System.Management.Instrumentation;
using System.Collections;

namespace J_Queen
{
    class Program
    {
        static void Main(string[] args)
        {
            Dictionary<string, ManagementObject> allDevices = new Dictionary<string, ManagementObject>();   //保存管理对象
            List<string> listDescription = new List<string>(); //保存网卡描述

            //获得管理类实例和管理对象
            ManagementClass classInstance = new ManagementClass( "Win32_NetworkAdapterConfiguration" );
            ManagementObjectCollection bjectCollection = classInstance.GetInstances();

            foreach (ManagementObject obj in objectCollection)
            {
                //如果没有启用IP设置的网络设备则跳过
                if (!(bool)obj[ "IPEnabled" ])
                {
                    continue;
                }

               //存储相关信息
                allDevices.Add((string)obj[ "Description" ], obj);
                listDescription.Add((string)obj[ "Description" ]);
            }
            for (int i = 0; i < listDescription.Count;i++ )
            {
                Console.WriteLine(i.ToString() + ": " + listDescription[i]);
            }
            Console.Write( "输入上面的数字编号(q:退出),选择网卡:" );
            string xInput = Console.ReadLine();
            int deviceNumber;
            while (xInput != "q" )
            {
                if(!Int32.TryParse(xInput, out deviceNumber))
                {
                    Console.Write( "输入错误,重新输入: " );
                    xInput=Console.ReadLine();
                    continue;
                }
                if (deviceNumber>listDescription.Count-1 || deviceNumber<0)
                {
                    Console.Write( "输入的编号超出范围,重新输入: " );
                    xInput=Console.ReadLine();
                    continue;
                }
                if (allDevices.ContainsKey(listDescription[deviceNumber]))
                {
                    Console.WriteLine( "1:表示释放IP(Release),2:表示重新获得IP(Renew)" );
                    string ptionInput = Console.ReadLine();
                    int option;
                    if(Int32.TryParse(optionInput, out option))
                    {
                        if(option ==1)
                        {
                            int returnValue = IpRelease((ManagementObject)allDevices[listDescription[deviceNumber]]);
                            if (returnValue<2)
                            {
                                Console.WriteLine( "成功释放IP" );
                                Console.Write( "输入上面网卡的数字编号(q:退出),选择网卡:" );
                                xInput=Console.ReadLine();
                                continue;
                            }
                        }
                        else if (option == 2)
                        {
                            int returnValue = IpRenew((ManagementObject)allDevices[listDescription[option]]);
                            if (returnValue<2)
                            {
                                Console.WriteLine( "成功分配IP" );
                                Console.Write( "输入上面网卡的数字编号(q:退出),选择网卡:" );
                                xInput = Console.ReadLine();
                                continue;
                            }
                        }
                        else
                        {
                            Console.WriteLine( "选择有误" );
                            Console.Write( "输入上面网卡的数字编号(q:退出),选择网卡:" );
                            Console.ReadLine();
                            continue;
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Function:重新分配指定网卡的IP
        /// </summary>
        /// <param name="obj">ManagementObject obj --对应网卡的管理对象</param>
        /// <returns>返回值,整数,0和1表示成功
</returns>
        public static int IpRenew(ManagementObject obj)
        {
            ManagementBaseObject utPar = null;
            utPar = obj.InvokeMethod( "RenewDHCPLease" , null, null);
            return Convert.ToInt32(outPar[ "returnValue" ]);
        }
        /// <summary>
        /// Function:释放指定网卡IP
        /// </summary>
        /// <param name="obj">ManagementObject obj--对应网卡的管理对象</param>
        /// <returns>返回值,整数,0和1表示成功
</returns>
        public static int IpRelease(ManagementObject obj)
        {
            ManagementBaseObject utPar = null;
            utPar = obj.InvokeMethod( "ReleaseDHCPLease" , null, null);
            return Convert.ToInt32(outPar[ "returnValue" ] );
        }
    }
}

最近在做一个东西,要用到动态分配IP和释放IP。在C++下很容易就可以实现。但是由于因为网络IP设置是要涉及到硬件,C#是没有现成接口调用的.只能通过调用API或者是WMI这道系统提供给我们的桥梁.主要是通过"Win32_NetworkAdapterConfiguration"这个管理类。

下面附上我写的一个Demo,希望对需要的朋友有帮助,更希望高手来拍砖。

using System;
using System.Collections.Generic;
using System.Text;
using System.Management;
using System.Management.Instrumentation;
using System.Collections;

namespace J_Queen
{
    class Program
    {
        static void Main(string[] args)
        {
            Dictionary<string, ManagementObject> allDevices = new Dictionary<string, ManagementObject>();   //保存管理对象
            List<string> listDescription = new List<string>(); //保存网卡描述

            //获得管理类实例和管理对象
            ManagementClass classInstance = new ManagementClass( "Win32_NetworkAdapterConfiguration" );
            ManagementObjectCollection bjectCollection = classInstance.GetInstances();

            foreach (ManagementObject obj in objectCollection)
            {
                //如果没有启用IP设置的网络设备则跳过
                if (!(bool)obj[ "IPEnabled" ])
                {
                    continue;
                }

               //存储相关信息
                allDevices.Add((string)obj[ "Description" ], obj);
                listDescription.Add((string)obj[ "Description" ]);
            }
            for (int i = 0; i < listDescription.Count;i++ )
            {
                Console.WriteLine(i.ToString() + ": " + listDescription[i]);
            }
            Console.Write( "输入上面的数字编号(q:退出),选择网卡:" );
            string xInput = Console.ReadLine();
            int deviceNumber;
            while (xInput != "q" )
            {
                if(!Int32.TryParse(xInput, out deviceNumber))
                {
                    Console.Write( "输入错误,重新输入: " );
                    xInput=Console.ReadLine();
                    continue;
                }
                if (deviceNumber>listDescription.Count-1 || deviceNumber<0)
                {
                    Console.Write( "输入的编号超出范围,重新输入: " );
                    xInput=Console.ReadLine();
                    continue;
                }
                if (allDevices.ContainsKey(listDescription[deviceNumber]))
                {
                    Console.WriteLine( "1:表示释放IP(Release),2:表示重新获得IP(Renew)" );
                    string ptionInput = Console.ReadLine();
                    int option;
                    if(Int32.TryParse(optionInput, out option))
                    {
                        if(option ==1)
                        {
                            int returnValue = IpRelease((ManagementObject)allDevices[listDescription[deviceNumber]]);
                            if (returnValue<2)
                            {
                                Console.WriteLine( "成功释放IP" );
                                Console.Write( "输入上面网卡的数字编号(q:退出),选择网卡:" );
                                xInput=Console.ReadLine();
                                continue;
                            }
                        }
                        else if (option == 2)
                        {
                            int returnValue = IpRenew((ManagementObject)allDevices[listDescription[option]]);
                            if (returnValue<2)
                            {
                                Console.WriteLine( "成功分配IP" );
                                Console.Write( "输入上面网卡的数字编号(q:退出),选择网卡:" );
                                xInput = Console.ReadLine();
                                continue;
                            }
                        }
                        else
                        {
                            Console.WriteLine( "选择有误" );
                            Console.Write( "输入上面网卡的数字编号(q:退出),选择网卡:" );
                            Console.ReadLine();
                            continue;
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Function:重新分配指定网卡的IP
        /// </summary>
        /// <param name="obj">ManagementObject obj --对应网卡的管理对象</param>
        /// <returns>返回值,整数,0和1表示成功
</returns>
        public static int IpRenew(ManagementObject obj)
        {
            ManagementBaseObject utPar = null;
            utPar = obj.InvokeMethod( "RenewDHCPLease" , null, null);
            return Convert.ToInt32(outPar[ "returnValue" ]);
        }
        /// <summary>
        /// Function:释放指定网卡IP
        /// </summary>
        /// <param name="obj">ManagementObject obj--对应网卡的管理对象</param>
        /// <returns>返回值,整数,0和1表示成功
</returns>
        public static int IpRelease(ManagementObject obj)
        {
            ManagementBaseObject utPar = null;
            utPar = obj.InvokeMethod( "ReleaseDHCPLease" , null, null);
            return Convert.ToInt32(outPar[ "returnValue" ] );
        }
    }
}


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#如何在Form中嵌入并且操作Excel表格发布时间:2022-07-10
下一篇:
word批量打印工具,c#写的发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap