首先引入服务 然后 调用
本文转载自http://blog.sina.com.cn/s/blog_7eeb43210101hf7f.html
public class Computer
{
public static string CpuID; //1.cpu序列号
public static string MacAddress; //2.mac序列号
public static string DiskID; //3.硬盘id
public static string IpAddress; //4.ip地址
public static string LoginUserName; //5.登录用户名
public static string ComputerName; //6.计算机名
public static string SystemType; //7.系统类型
public static string TotalPhysicalMemory; //8.内存量 单位:M
static Computer()
{
CpuID = GetCpuID();
MacAddress = GetMacAddress();
DiskID = GetDiskID();
IpAddress = GetIPAddress();
LoginUserName = GetUserName();
SystemType = GetSystemType();
TotalPhysicalMemory = GetTotalPhysicalMemory();
ComputerName = GetComputerName();
}
//1.获取CPU序列号代码
static string GetCpuID()
{
try
{
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
{
}
}
//2.获取网卡硬件地址
static 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
{
}
}
//3.获取硬盘ID
static 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"].Value;
}
moc = null;
mc = null;
return HDid;
}
catch
{
return "unknow";
}
finally
{
}
}
//4.获取IP地址
static string GetIPAddress()
{
try
{
string st = "";
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
if ((bool)mo["IPEnabled"] == true)
{
//st=mo["IpAddress"].ToString();
System.Array ar;
ar = (System.Array)(mo.Properties["IpAddress"].Value);
st = ar.GetValue(0).ToString();
break;
}
}
moc = null;
mc = null;
return st;
}
catch
{
return "unknow";
}
finally
{
}
}
/// 5.操作系统的登录用户名
static string GetUserName()
{
try
{
string un = "";
un = Environment.UserName;
return un;
}
catch
{
return "unknow";
}
finally
{
}
}
//6.获取计算机名
static string GetComputerName()
{
try
{
return System.Environment.MachineName;
}
catch
{
return "unknow";
}
finally
{
}
}
///7 PC类型
static 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
{
}
}
///8.物理内存
static 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
{
}
}
}
我们在利用C#开发桌面程序(Winform)程序的时候,经常需要获取一些跟系统相关的信息,例如用户名、MAC地址、IP地址、硬盘ID、CPU序列号、系统名称、物理内存等。
首先需要引入命名空间:
-
-
- public class GetSystemInfo
- {
-
-
-
-
- public static string GetUserName()
- {
- try
- {
- string strUserName = string.Empty;
- ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
- ManagementObjectCollection moc = mc.GetInstances();
- foreach (ManagementObject mo in moc)
- {
- strUserName = mo["UserName"].ToString();
- }
- moc = null;
- mc = null;
- return strUserName;
- }
- catch
- {
- return "unknown";
- }
- }
-
-
-
-
- public static string GetMacAddress()
- {
- try
- {
- string strMac = string.Empty;
- ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
- ManagementObjectCollection moc = mc.GetInstances();
- foreach (ManagementObject mo in moc)
- {
- if ((bool)mo["IPEnabled"] == true)
- {
- strMac = mo["MacAddress"].ToString();
- }
- }
- moc = null;
- mc = null;
- return strMac;
- }
- catch
- {
- return "unknown";
- }
- }
-
-
-
-
- public static string getMacAddr_Local()
- {
- string madAddr = null;
- try
- {
- ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
- ManagementObjectCollection moc2 = mc.GetInstances();
- foreach (ManagementObject mo in moc2)
- {
- if (Convert.ToBoolean(mo["IPEnabled"]) == true)
- {
- madAddr = mo["MacAddress"].ToString();
- madAddr = madAddr.Replace(':', '-');
- }
- mo.Dispose();
- }
- if (madAddr == null)
- {
- return "unknown";
- }
- else
- {
- return madAddr;
- }
- }
- catch (Exception)
- {
- return "unknown";
- }
- }
-
-
-
-
- public static string GetClientLocalIPv6Address()
- {
- string strLocalIP = string.Empty;
- try
- {
- IPHostEntry ipHost = Dns.GetHostEntry(Dns.GetHostName());
- IPAddress ipAddress = ipHost.AddressList[0];
- strLocalIP = ipAddress.ToString();
- return strLocalIP;
- }
- catch
- {
- return "unknown";
- }
- }
-
-
-
-
- public static string GetClientLocalIPv4Address()
- {
- string strLocalIP = string.Empty;
- try
- {
- IPHostEntry ipHost = Dns.Resolve(Dns.GetHostName());
- IPAddress ipAddress = ipHost.AddressList[0];
- strLocalIP = ipAddress.ToString();
- return strLocalIP;
- }
- catch
- {
- return "unknown";
- }
- }
-
-
-
-
- public static List<string> GetClientLocalIPv4AddressList()
- {
- List<string> ipAddressList = new List<string>();
- try
- {
- IPHostEntry ipHost = Dns.Resolve(Dns.GetHostName());
- foreach (IPAddress ipAddress in ipHost.AddressList)
- {
- if (!ipAddressList.Contains(ipAddress.ToString()))
- {
- ipAddressList.Add(ipAddress.ToString());
- }
- }
- }
- catch
- {
-
- }
- return ipAddressList;
- }
-
-
-
-
-
- public static string GetClientInternetIPAddress()
- {
- string strInternetIPAddress = string.Empty;
- try
- {
- using (WebClient webClient = new WebClient())
- {
- strInternetIPAddress = webClient.DownloadString("http://www.coridc.com/ip");
- Regex r = new Regex("[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}");
- Match mth = r.Match(strInternetIPAddress);
- if (!mth.Success)
- {
- strInternetIPAddress = GetClientInternetIPAddress2();
- mth = r.Match(strInternetIPAddress);
- if (!mth.Success)
- {
- strInternetIPAddress = "unknown";
- }
- }
- return strInternetIPAddress;
- }
- }
- catch
- {
- return "unknown";
- }
- }
-
-
-
-
- private static string GetClientInternetIPAddress2()
- {
- string tempip = "";
- try
- {
-
- WebRequest wr = WebRequest.Create("http://iframe.ip138.com/ic.asp");
- Stream s = wr.GetResponse().GetResponseStream();
- StreamReader sr = new StreamReader(s, Encoding.Default);
- string all = sr.ReadToEnd();
-
- int start = all.IndexOf("[") + 1;
- int end = all.IndexOf("]", start);
- tempip = all.Substring(start, end - start);
- sr.Close();
- s.Close();
- return tempip;
- }
- catch
- {
- return "unknown";
- }
- }
-
-
-
-
- public static string GetDiskID()
- {
- try
- {
- string strDiskID = string.Empty;
- ManagementClass mc = new ManagementClass("Win32_DiskDrive");
- ManagementObjectCollection moc = mc.GetInstances();
- foreach (ManagementObject mo in moc)
- {
- strDiskID = mo.Properties["Model"].Value.ToString();
- }
- moc = null;
- mc = null;
- return strDiskID;
- }
- catch
- {
- return "unknown";
- }
- }
-
-
-
-
- public static string GetCpuID()
- {
- try
- {
- string strCpuID = string.Empty;
- ManagementClass mc = new ManagementClass("Win32_Processor");
- ManagementObjectCollection moc = mc.GetInstances();
- foreach (ManagementObject mo in moc)
- {
- strCpuID = mo.Properties["ProcessorId"].Value.ToString();
- }
- moc = null;
- mc = null;
- return strCpuID;
- }
- catch
- {
- return "unknown";
- }
- }
-
-
-
-
- public static string GetSystemType()
- {
- try
- {
- string strSystemType = string.Empty;
- ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
- ManagementObjectCollection moc = mc.GetInstances();
- foreach (ManagementObject mo in moc)
- {
- strSystemType = mo["SystemType"].ToString();
- }
- moc = null;
- mc = null;
- return strSystemType;
- }
- catch
- {
- return "unknown";
- }
- }
-
-
-
-
- public static string GetSystemName()
- {
- try
- {
- string strSystemName = string.Empty;
- ManagementObjectSearcher mos = new ManagementObjectSearcher("root\\CIMV2", "SELECT PartComponent FROM Win32_SystemOperatingSystem");
- foreach (ManagementObject mo in mos.Get())
- {
- strSystemName = mo["PartComponent"].ToString();
- }
- mos = new ManagementObjectSearcher("root\\CIMV2", "SELECT Caption FROM Win32_OperatingSystem");
- foreach (ManagementObject mo in mos.Get())
- {
- strSystemName = mo["Caption"].ToString();
- }
- return strSystemName;
- }
- catch
- {
- return "unknown";
- }
- }
-
-
-
-
- public static string GetTotalPhysicalMemory()
- {
- try
- {
- string strTotalPhysicalMemory = string.Empty;
- ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
- ManagementObjectCollection moc = mc.GetInstances();
- foreach (ManagementObject mo in moc)
- {
- strTotalPhysicalMemory = mo["TotalPhysicalMemory"].ToString();
- }
- moc = null;
- mc = null;
- return strTotalPhysicalMemory;
- }
- catch
- {
- return "unknown";
- }
- }
-
-
-
-
-
- public static string GetMotherBoardID()
- {
- try
- {
- ManagementClass mc = new ManagementClass("Win32_BaseBoard");
- ManagementObjectCollection moc = mc.GetInstances();
- string strID = null;
- foreach (ManagementObject mo in moc)
- {
- strID = mo.Properties["SerialNumber"].Value.ToString();
- break;
- }
- return strID;
- }
- catch
- {
- return "unknown";
- }
- }
-
-
-
- public static string GetAllUsersDesktopFolderPath()
- {
- RegistryKey folders;
- folders = OpenRegistryPath(Registry.LocalMachine, @"/software/microsoft/windows/currentversion/explorer/shell folders");
- string desktopPath = folders.GetValue("Common Desktop").ToString();
- return desktopPath;
- }
-
-
-
- public static string GetAllUsersStartupFolderPath()
- {
- RegistryKey folders;
- folders = OpenRegistryPath(Registry.LocalMachine, @"/software/microsoft/windows/currentversion/explorer/shell folders");
- string Startup = folders.GetValue("Common Startup").ToString();
- return Startup;
- }
- private static RegistryKey OpenRegistryPath(RegistryKey root, string s)
- {
- s = s.Remove(0, 1) + @"/";
- while (s.IndexOf(@"/") != -1)
- {
- root = root.OpenSubKey(s.Substring(0, s.IndexOf(@"/")));
- s = s.Remove(0, s.IndexOf(@"/") + 1);
- }
- return root;
- }
- private void Test()
- {
- RegistryKey folders;
- folders = OpenRegistryPath(Registry.LocalMachine, @"/software/microsoft/windows/currentversion/explorer/shell folders");
-
- string desktopPath = folders.GetValue("Common Desktop").ToString();
-
- string fontsPath = folders.GetValue("Fonts").ToString();
-
- string nethoodPath = folders.GetValue("Nethood").ToString();
-
- string personalPath = folders.GetValue("Personal").ToString();
-
- string programsPath = folders.GetValue("Programs").ToString();
-
- string recentPath = folders.GetValue("Recent").ToString();
-
- string sendtoPath = folders.GetValue("Sendto").ToString();
-
- string startmenuPath = folders.GetValue("Startmenu").ToString();
-
- string startupPath = folders.GetValue("Startup").ToString();
-
- string favoritesPath = folders.GetValue("Favorites").ToString();
-
- string historyPath = folders.GetValue("History").ToString();
-
- string cookiesPath = folders.GetValue("Cookies").ToString();
-
- string cachePath = folders.GetValue("Cache").ToString();
-
- string appdataPath = folders.GetValue("Appdata").ToString();
-
- string printhoodPath = folders.GetValue("Printhood").ToString();
- }</span>
来自小勇.NET博客:http://blog.csdn.net/xiaoyong_net