c#获取域名、IP地址、MAC地址、电脑所有进程、电脑信息

1、获取域名

//获取域名
private string getDomain()
{
    string Domain = Environment.UserDomainName;
    SelectQuery query = new SelectQuery("Win32_ComputerSystem");
    using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(query))
    {
        foreach (ManagementObject mo in searcher.Get())
        {
            if ((bool)mo["partofdomain"])
            {
                Domain = mo["domain"].ToString();
                break;
            }
        }
    }
    Domain = Domain.ToUpper();
    return Domain;
}

2、获取IP地址

//获取本机的IP地址
public string getIPAddr()
{
    string IPAddr = string.Empty;
    try
    {
        IPHostEntry IPHE = new IPHostEntry();
        IPHE = Dns.GetHostEntry(Dns.GetHostName());
        for (int i = 0; i < IPHE.AddressList.Length; i++)
        {
            if (IPHE.AddressList[i].ToString().Length < 7)
            {
                continue;
            }
            if (IPHE.AddressList[i].ToString().IndexOf(":") > 0)
            {
                continue;
            }
            IPAddr = IPAddr + IPHE.AddressList[i].ToString() + ";";
        }
    }
    catch (Exception e)
    {
        return "";
    }
    return IPAddr;
}

3、获取MAC地址

//获取MAC地址
public string getMacAddr()
{
    string mac = string.Empty;
    SelectQuery query = new SelectQuery("Win32_NetworkAdapterConfiguration");
    using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(query))
    {
        ManagementObjectCollection queryCollection = searcher.Get();
        foreach (ManagementObject mo in searcher.Get())
        {
            if (mo["IPEnabled"].ToString() == "True")
            {
                mac = mo["MacAddress"].ToString();
                break;
            }
        }
    }
    return mac;
}

4、获取电脑线程

 Process[] processes = Process.GetProcesses();

5、获取电脑信息

var os = new ComputerInfo();

 

c#获取域名、IP地址、MAC地址、电脑所有进程、电脑信息

上一篇:16.敏捷相关知识


下一篇:09-redis的五种数据结构