C#.NET GetLocalMachineInfo.cs

 public string[] GetLocalMachineInfo(string Type)
        {
            switch (Type.ToUpper())
            {
                case "IP":
                    IPHostEntry IPHE = new IPHostEntry();
                    IPHE = Dns.GetHostEntry(Dns.GetHostName());
                    string[] resultIP = new string[IPHE.AddressList.Length];
                    for (int i = 0; i < IPHE.AddressList.Length; i++)
                    {
                        resultIP[i] = IPHE.AddressList[i].ToString();
                    }
                    return resultIP;
                    break;
                case "MAC":
                    ManagementClass mc;
                    ManagementObjectCollection moc;
                    mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
                    moc = mc.GetInstances();
                    int j = 0;
                    foreach (ManagementObject mo in moc)
                    {
                        if ((bool)mo["IPEnabled"] == true)
                        {
                            j = j + 1;
                        }
                    }
                    string[] resultMac = new string[j];
                    j = 0;
                    foreach (ManagementObject mo in moc)
                    {
                        if ((bool)mo["IPEnabled"] == true)
                        {
                            resultMac[j] = mo["MacAddress"].ToString().Replace(":", "");
                            j = j + 1;
                        }
                    }
                    return resultMac;
                    break;
                case "HDD":
                    ManagementObject disk;
                    disk = new ManagementObject("win32_logicaldisk.deviceid=\"c:\"");
                    disk.Get();
                    string[] resultHDD = new string[1];
                    resultHDD[0] = disk.GetPropertyValue("VolumeSerialNumber").ToString();
                    return resultHDD;
                    break;
                case "USER":
                    ManagementClass mcUser = new ManagementClass("Win32_ComputerSystem");
                    ManagementObjectCollection mocUser = mcUser.GetInstances();
                    int k = 0;
                    foreach (ManagementObject mo in mocUser)
                    {
                        k = k + 1;
                    }
                    string[] resultUser = new string[k];
                    k = 0;
                    foreach (ManagementObject mo in mocUser)
                    {
                        resultUser[k] = mo["UserName"].ToString();

                        k = k + 1;
                    }
                    return resultUser;
                    break;
                default:
                    string[] resultNull = new string[1];
                    return resultNull;
                    break;
            }
        }

posted on 2008-11-28 18:32    阅读(285)  评论(0编辑  收藏  举报