程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> 用C#獲取CPU編號、硬盤編號等系統有關環境、屬性(2)

用C#獲取CPU編號、硬盤編號等系統有關環境、屬性(2)

編輯:關於C語言

另外還定義了一個調用類SystemInfo.cs,代碼如下:

using System;
using System.Configuration;
using System.Runtime.InteropServices;
using System.Management;
using System.Text;

/**//// <summary>
/// SystemInfo 的摘要說明
/// </summary>
public class SystemInfo
...{
private const int CHAR_COUNT = 128;
public SystemInfo()
...{

}
[DllImport("kernel32")]
private static extern void GetWindowsDirectory(StringBuilder WinDir, int count);

[DllImport("kernel32")]
private static extern void GetSystemDirectory(StringBuilder SysDir, int count);

[DllImport("kernel32")]
private static extern void GetSystemInfo(ref CpuInfo cpuInfo);

[DllImport("kernel32")]
private static extern void GlobalMemoryStatus(ref MemoryInfo memInfo);

[DllImport("kernel32")]
private static extern void GetSystemTime(ref SystemTimeInfo sysInfo);

/**//// <summary>
/// 查詢CPU編號
/// </summary>
/// <returns></returns>
public string GetCpuId()
...{
ManagementClass mClass = new ManagementClass("Win32_Processor");
ManagementObjectCollection moc = mClass.GetInstances();
string cpuId=null;
foreach (ManagementObject mo in moc)
...{
cpuId = mo.PropertIEs["ProcessorId"].Value.ToString();
break;
}
return cpuId;
}

/**//// <summary>
/// 查詢硬盤編號
/// </summary>
/// <returns></returns>
public string GetMainHardDiskId()
...{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");
String hardDiskID=null;
foreach (ManagementObject mo in searcher.Get())
...{
hardDiskID = mo["SerialNumber"].ToString().Trim();
break;
}
return hardDiskID;
}

/**//// <summary>
/// 獲取Windows目錄
/// </summary>
/// <returns></returns>
public string GetWinDirectory()
...{
StringBuilder sBuilder = new StringBuilder(CHAR_COUNT);
GetWindowsDirectory(sBuilder, CHAR_COUNT);
return sBuilder.ToString();
}

/**//// <summary>
/// 獲取系統目錄
/// </summary>
/// <returns></returns>
public string GetSysDirectory()
...{
StringBuilder sBuilder = new StringBuilder(CHAR_COUNT);
GetSystemDirectory(sBuilder, CHAR_COUNT);
return sBuilder.ToString();
}

/**//// <summary>
/// 獲取CPU信息
/// </summary>
/// <returns></returns>
public CpuInfo GetCpuInfo()
...{
CpuInfo cpuInfo = new CpuInfo();
GetSystemInfo(ref cpuInfo);
return cpuInfo;
}

/**//// <summary>
/// 獲取系統內存信息
/// </summary>
/// <returns></returns>
public MemoryInfo GetMemoryInfo()
...{
MemoryInfo memoryInfo = new MemoryInfo();
GlobalMemoryStatus(ref memoryInfo);
return memoryInfo;
}

/**//// <summary>
/// 獲取系統時間信息
/// </summary>
/// <returns></returns>
public SystemTimeInfo GetSystemTimeInfo()
...{
SystemTimeInfo systemTimeInfo = new SystemTimeInfo();
GetSystemTime(ref systemTimeInfo);
return systemTimeInfo;
}

/**//// <summary>
/// 獲取系統名稱
/// </summary>
/// <returns></returns>
public string GetOperationSystemInName()
...{
OperatingSystem os = System.Environment.OSVersion;
string osName = "UNKNOWN";
switch (os.Platform)
...{
case PlatformID.Win32Windows:
switch (os.Version.Minor)
...{
case 0: osName = "Windows 95"; break;
case 10: osName = "Windows 98"; break;
case 90: osName = "Windows ME"; break;
}
break;
case PlatformID.Win32NT:
switch (os.Version.Major)
...{
case 3: osName = "Windws NT 3.51"; break;
case 4: osName = "Windows NT 4"; break;
case 5: if (os.Version.Minor == 0)
...{
osName = "Windows 2000";
}
else if (os.Version.Minor == 1)
...{
osName = "Windows XP";
}
else if (os.Version.Minor == 2)
...{
osName = "Windows Server 2003";
}
break;
case 6: osName = "Longhorn"; break;
}
break;
}
return String.Format("{0},{1}", osName, os.Version.ToString());
}
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved