整理 C# 獲取系統環境與屬性的常用方法,提供參考。需定義結構體供 DllImport 回傳參數。
[StructLayout(LayoutKind.Sequential)]
public struct CpuInfo {
public uint dwOemId; // OEM ID
public uint dwPageSize; // 頁面大小
public uint lpMinimumApplicationAddress;
public uint lpMaximumApplicationAddress;
public uint dwActiveProcessorMask;
public uint dwNumberOfProcessors; // CPU 個數
public uint dwProcessorType; // CPU 類型
public uint dwAllocationGranularity;
public uint dwProcessorLevel; // CPU 等級
public uint dwProcessorRevision;
}
[StructLayout(LayoutKind.Sequential)]
public struct MemoryInfo {
public uint dwLength;
public uint dwMemoryLoad; // 已使用記憶體
public uint dwTotalPhys; // 總物理記憶體
public uint dwAvailPhys; // 可用物理記憶體
public uint dwTotalPageFile; // 交換檔總大小
public uint dwAvailPageFile; // 可用交換檔
public uint dwTotalVirtual; // 總虛擬記憶體
public uint dwAvailVirtual; // 可用虛擬記憶體
}
[StructLayout(LayoutKind.Sequential)]
public struct SystemTimeInfo {
public ushort wYear; wMonth; wDayOfWeek; wDay; wHour; wMinute; wSecond; wMilliseconds;
}
P/Invoke 系統 API 並封裝 WMI 查詢:
public class SystemInfo {
private const int CHAR_COUNT = 128;
[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);
// WMI 查詢 CPU ID
public string GetCpuId() {
ManagementClass mClass = new ManagementClass("Win32_Processor");
foreach (ManagementObject mo in mClass.GetInstances()) {
return mo.Properties["ProcessorId"].Value.ToString();
}
return null;
}
// WMI 查詢硬碟序號
public string GetMainHardDiskId() {
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");
foreach (ManagementObject mo in searcher.Get()) {
return mo["SerialNumber"].ToString().Trim();
}
return null;
}
public string GetWinDirectory() { var sb = new StringBuilder(CHAR_COUNT); GetWindowsDirectory(sb, CHAR_COUNT); return sb.ToString(); }
public string GetSysDirectory() { var sb = new StringBuilder(CHAR_COUNT); GetSystemDirectory(sb, CHAR_COUNT); return sb.ToString(); }
public CpuInfo GetCpuInfo() { var ci = new CpuInfo(); GetSystemInfo(ref ci); return ci; }
public MemoryInfo GetMemoryInfo() { var mi = new MemoryInfo(); GlobalMemoryStatus(ref mi); return mi; }
public SystemTimeInfo GetSystemTimeInfo() { var st = new SystemTimeInfo(); GetSystemTime(ref st); return st; }
public string GetOperationSystemInName() {
OperatingSystem os = 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 = "Windows 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);
}
}
ASPX 頁面輸出(WinForm 同理):
SystemInfo si = new SystemInfo();
Response.Write("作業系統:" + si.GetOperationSystemInName() + "<br>");
Response.Write("CPU編號:" + si.GetCpuId() + "<br>");
Response.Write("硬碟編號:" + si.GetMainHardDiskId() + "<br>");
// ... 輸出記憶體、CPU 詳細資訊
前台無需控件,直接輸出。