c#中如何获取本机用户名、MAC地址、IP地址、硬盘ID、CPU序列号、系统名称、物理内存...

//c#中如何获取本机用户名、MAC地址、IP地址、硬盘ID、CPU序列号、系统名称、物理内存。


using System.Management;

/// 操作系统的登录用户名:
string GetUserName()
{
    try
    {
        string st = "";
        ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
        ManagementObjectCollection moc = mc.GetInstances();
        foreach (ManagementObject mo in moc)
        {
             st = mo["UserName"].ToString();
        }
        moc = null;
        mc = null;
        return st;
    }
    catch
    {
        return "unknow";
    }
    finally
    {   }
 }
//获取MAC地址:

string GetMacAddress()
{
    try
    {
        //获取网卡硬件地址
        string mac = "";
        ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
        ManagementObjectCollection moc = mc.GetInstances();
        foreach (ManagementObject mo in moc)
        {
            if ((bool)mo["IPEnabled"] == true)
            {
                mac = mo["MacAddress"].ToString();
                break;
            }
        }
        moc = null;
        mc = null;
        return mac;
    }
    catch
    {
        return "unknow";
    }
    finally
    {   }
}
//获取IP地址:

        /// <summary>
        /// 获取客户端内网IP地址
        /// </summary>
        /// <returns></returns>
        private static string getClientLocalIPAddress()
        {
            string localIP = null;
            try
            {
                IPHostEntry ipHost = System.Net.Dns.GetHostEntry(Dns.GetHostName());// Dns.Resolve(Dns.GetHostName()); ;
                IPAddress ipaddress ;
                for (int i = 0; i <  ipHost.AddressList.Length; i++)
                {
                    ipaddress = ipHost.AddressList[i];
                    if (ipaddress.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)   
                    {
                        localIP = ipaddress.ToString();
                        break;
                    }
                }
 
                //IPAddress.AddressFamily 属性
                //对于 IPv4,返回 InterNetwork;对于 IPv6,返回 InterNetworkV6。
                if (localIP==null)
                {
                    localIP = ipHost.AddressList[0].ToString();
                }
                return localIP;
            }
            catch
            {
                return "内网IP地址:unknown";
            }
            finally
            { }
        }  
        
         /// <summary>
        /// 获得客户端外网IP地址
        /// </summary>
        /// <returns>IP地址</returns>
        public static string getClientInternetIPAddress()
        {
            string internetAddress = "";
            try
            {
                using (WebClient webClient = new WebClient())
                {
                    internetAddress = webClient.DownloadString("http://www.coridc.com/ip");//从外部网页获得IP地址
                    //判断IP是否合法
                    if (!System.Text.RegularExpressions.Regex.IsMatch(internetAddress, "[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}"))
                    {
                        internetAddress = webClient.DownloadString("http://fw.qq.com/ipaddress");//从腾讯提供的API中获得IP地址
                    }
                }
                return "外网IP地址:" + internetAddress;
            }
            catch
            {
                return "外网IP地址:unknown";
            }
            finally
            {   }
        }
//获取硬盘ID:

string GetDiskID()
{
    try
    {
        String HDid = "";
        ManagementClass mc = new ManagementClass("Win32_DiskDrive");
        ManagementObjectCollection moc = mc.GetInstances();
        foreach (ManagementObject mo in moc)
        {
            //HDid = (string)mo.Properties["Model"].ToString();
            HDid = (String)mo.Properties["Model"].Value.ToString();
        }
        moc = null;
        mc = null;
        return HDid;
    }
    catch
    {
        return "unknow";
    }
    finally
    {   }
}
//获取CPUid:

string GetCpuID()
{
    try
    {
        //获取CPU序列号代码
        string cpuInfo = "";//cpu序列号
        ManagementClass mc = new ManagementClass("Win32_Processor");
        ManagementObjectCollection moc = mc.GetInstances();
        foreach (ManagementObject mo in moc)
        {
            cpuInfo = mo.Properties["ProcessorId"].Value.ToString();
        }
        moc = null;
        mc = null;
        return cpuInfo;
    }
    catch
    {
        return "unknow";
    }
    finally
    {   }
}
//系统名称:

string GetSystemType()
{
    try
    {
        string st = "";
        ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
        ManagementObjectCollection moc = mc.GetInstances();
        foreach (ManagementObject mo in moc)
        {
            st = mo["SystemType"].ToString();
        }
        moc = null;
        mc = null;
        return st;
    }
    catch
    {
        return "unknow";
    }
    finally
    {   }
}
//获取物理内存:

string GetTotalPhysicalMemory()
{
    try
    {
        string st = "";
        ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
        ManagementObjectCollection moc = mc.GetInstances();
        foreach (ManagementObject mo in moc)
        {
            st = mo["TotalPhysicalMemory"].ToString();
        }
        moc = null;
        mc = null;
        return st;
    }
    catch
    {
        return "unknow";
    }
    finally
    {   }
}

 

转载于:https://www.cnblogs.com/cgdou/archive/2013/05/07/3065270.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值