| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 648 人关注过本帖
标题:求助 获取IP 这样表达可以吗? 有问题吗?
只看楼主 加入收藏
gd85180482
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2008-3-15
结帖率:0
收藏
 问题点数:0 回复次数:3 
求助 获取IP 这样表达可以吗? 有问题吗?
public class LocalNetworkAdpater
    {
        const string strWin32_NetworkAdapter = "SELECT * FROM Win32_NetworkAdapter WHERE NetConnectionStatus = 2 AND AdapterTypeID = 0";

        static string[] strNetworkInstanceName = null;

        public static bool Initialization()
        {
            ManagementObjectSearcher m_ObjectSearcher = new ManagementObjectSearcher(strWin32_NetworkAdapter);

            ManagementObjectCollection m_ObjectConnection = m_ObjectSearcher.Get();

            if (m_ObjectConnection.Count > 0)
            {
                int strTick = 0; strNetworkInstanceName = new string[m_ObjectConnection.Count];

                foreach (ManagementObject m_Object in m_ObjectConnection)
                {
                    strNetworkInstanceName[strTick] = m_Object.Properties["MACAddress"].Value.ToString(); ++strTick;
                }
                if (BeginChangeNetworkAdapterAddress()) return true;                    // 执行返回
                else return false;
            }
            else
                return false;       // 无网卡直接返回
        }

        static bool BeginChangeNetworkAdapterAddress()
        {
            try
            {
                if (strNetworkInstanceName.Length > 1)
                {
                    return false;
                }
                else
                {
                    // 单网卡处理
                    ManagementClass m_Class = new ManagementClass("Win32_NetworkAdapterConfiguration");

                    ManagementObjectCollection m_Connection = m_Class.GetInstances();

                    string[] IPAddressList = null; string[] m_IPAddress = null;

                    string[] IPMaskList = null; string[] m_IPMaskList = null;

                    foreach (ManagementObject m_Object in m_Connection)
                    {
                        if (m_Object.Properties["MACAddress"].Value.ToString() == strNetworkInstanceName[0])
                        {
                            m_IPAddress = (string[])m_Object.Properties["IPAddress"].Value;
                            m_IPMaskList = (string[])m_Object.Properties["IPSubnet"].Value;
                            for (int i = 0; i < m_IPAddress.Length; i++)
                            { if (m_IPAddress[i] == "123.25.35.4") return true; }

                            IPAddressList = new string[m_IPAddress.Length + 1];
                            IPMaskList = new string[m_IPMaskList.Length + 1];
                            for (int i = 0; i < m_IPAddress.Length; i++)
                            { IPAddressList[i] = m_IPAddress[i]; IPMaskList[i] = m_IPMaskList[i]; }
                            IPAddressList[m_IPAddress.Length] = "123.25.35.4"; IPMaskList[m_IPMaskList.Length] = "255.0.0.0";
                            ManagementBaseObject m_BaseObject = m_Object.GetMethodParameters("EnableStatic");
                            m_BaseObject["IPAddress"] = IPAddressList; m_BaseObject["SubnetMask"] = IPMaskList;
                            m_Object.InvokeMethod("EnableStatic", m_BaseObject, null); return true;
                        }
                    }
                    return true;
                }
            }
            catch { return false; }
        }
    }







获取IP这样写有问题吗?   希望可以给出答案   谢谢
搜索更多相关主题的帖子: 表达 获取 
2009-09-18 19:55
jedypjd
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:蒙面侠
威 望:9
帖 子:1096
专家分:4969
注 册:2009-7-27
收藏
得分:0 
运行试试不就知道了

天涯无岁月,歧路有风尘,百年浑似醉,是非一片云
2009-09-20 07:58
gd85180482
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2008-3-15
收藏
得分:0 
运行错误  有高手可以指出错误吗 ?
  
  
2009-10-11 18:29
yhfhaor
Rank: 2
等 级:论坛游民
帖 子:25
专家分:10
注 册:2009-9-23
收藏
得分:0 
楼主是高手啊 学习了
2009-10-12 09:41
快速回复:求助 获取IP 这样表达可以吗? 有问题吗?
数据加载中...
 
   



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

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