如果利用C#獲取系統有關環境和屬性,這個也是在網上問得比較多的問題,不過大部分只有提問沒有回答,最近正好想做有關方面的東西,整理了一下,提供給大家,希望能給大家提供參考意見:
首先需要定義幾個結構(struct) ,便於DllImport作為返回參數調用。以下是代碼:
CpuInfo.cs
using System;
using System.Configuration;
using System.Runtime.InteropServices;
/**//**
* LayoutKind.Automatic:為了提高效率允許運行態對類型成員重新排序
* 注意:永遠不要使用這個選項來調用不受管轄的動態鏈接庫函數。
* LayoutKind.Explicit:對每個域按照FieldOffset屬性對類型成員排序
* LayoutKind.Sequential:對出現在受管轄類型定義地方的不受管轄內存中的類型成員進行排序。
*/
/**//// <summary>
/// 定義CPU的信息結構
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct CpuInfo
...{
/**//// <summary>
/// OEM ID
/// </summary>
public uint dwOemId;
/**//// <summary>
/// 頁面大小
/// </summary>
public uint dwPageSize;
public uint lpMinimumApplicationAddress;
public uint lpMaximumApplicationAddress;
public uint dwActiveProcessorMask;
/**//// <summary>
/// CPU個數
/// </summary>
public uint dwNumberOfProcessors;
/**//// <summary>
/// CPU類型
/// </summary>
public uint dwProcessorType;
public uint dwAllocationGranularity;
/**//// <summary>
/// CPU等級
/// </summary>
public uint dwProcessorLevel;
public uint dwProcessorRevision;
}
MemoryInfo.cs
using System;
using System.Configuration;
using System.Runtime.InteropServices;
/**//**
* LayoutKind.Automatic:為了提高效率允許運行態對類型成員重新排序
* 注意:永遠不要使用這個選項來調用不受管轄的動態鏈接庫函數。
* LayoutKind.Explicit:對每個域按照FieldOffset屬性對類型成員排序
* LayoutKind.Sequential:對出現在受管轄類型定義地方的不受管轄內存中的類型成員進行排序。
*/
/**//// <summary>
/// 定義內存的信息結構
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct MemoryInfo
...{
/**//// <summary>
///
/// </summary>
public uint dwLength;
/**//// <summary>
/// 已經使用的內存
/// </summary>
public uint dwMemoryLoad;
/**//// <summary>
/// 總物理內存大小
/// </summary>
public uint dwTotalPhys;
/**//// <summary>
/// 可用物理內存大小
/// </summary>
public uint dwAvailPhys;
/**//// <summary>
/// 交換文件總大小
/// </summary>
public uint dwTotalPageFile;
/**//// <summary>
/// 可用交換文件大小
/// </summary>
public uint dwAvailPageFile;
/**//// <summary>
/// 總虛擬內存大小
/// </summary>
public uint dwTotalVirtual;
/**//// <summary>
/// 可用虛擬內存大小
/// </summary>
public uint dwAvailVirtual;
}
SystemTimeInfo.cs
using System;
using System.Configuration;
using System.Runtime.InteropServices;
/**//**
* LayoutKind.Automatic:為了提高效率允許運行態對類型成員重新排序
* 注意:永遠不要使用這個選項來調用不受管轄的動態鏈接庫函數。
* LayoutKind.Explicit:對每個域按照FieldOffset屬性對類型成員排序
* LayoutKind.Sequential:對出現在受管轄類型定義地方的不受管轄內存中的類型成員進行排序。
*/
/**//// <summary>
/// 定義系統時間的信息結構
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct SystemTimeInfo
...{
/**//// <summary>
/// 年
/// </summary>
public ushort wYear;
/**//// <summary>
/// 月
/// </summary>
public ushort wMonth;
/**//// <summary>
/// 星期
/// </summary>
public ushort wDayOfWeek;
/**//// <summary>
/// 天
/// </summary>
public ushort wDay;
/**//// <summary>
/// 小時
/// </summary>
public ushort wHour;
/**//// <summary>
/// 分鐘
/// </summary>
public ushort wMinute;
/**//// <summary>
/// 秒
/// </summary>
public ushort wSecond;
/**//// <summary>
/// 毫秒
/// </summary>
public ushort wMilliseconds;
}
另外還定義了一個調用類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());
}
}
以下是調用實例,為了簡單,我在一個aspx頁面中輸出,不過這個程序可以在WinForm中調用:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Specialized;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Runtime.InteropServices;
public partial class Index : System.Web.UI.Page
...{
protected void Page_Load(object sender, EventArgs e)
...{
if (!Page.IsPostBack)
...{
SystemInfo systemInfo = new SystemInfo();
Response.Write("操作系統:" + systemInfo.GetOperationSystemInName() + "<br>");
Response.Write("CPU編號:"+systemInfo.GetCpuId() + "<br>");
Response.Write("硬盤編號:"+systemInfo.GetMainHardDiskId() + "<br>");
Response.Write("Windows目錄所在位置:" + systemInfo.GetSysDirectory() + "<br>");
Response.Write("系統目錄所在位置:" + systemInfo.GetWinDirectory() + "<br>");
MemoryInfo memoryInfo = systemInfo.GetMemoryInfo();
CpuInfo cpuInfo = systemInfo.GetCpuInfo();
Response.Write("dwActiveProcessorMask" + cpuInfo.dwActiveProcessorMask + "<br>");
Response.Write("dwAllocationGranularity" + cpuInfo.dwAllocationGranularity + "<br>");
Response.Write("CPU個數:" + cpuInfo.dwNumberOfProcessors + "<br>");
Response.Write("OEM ID:" + cpuInfo.dwOemId + "<br>");
Response.Write("頁面大小" + cpuInfo.dwPageSize + "<br>");
Response.Write("CPU等級" + cpuInfo.dwProcessorLevel + "<br>");
Response.Write("dwProcessorRevision" + cpuInfo.dwProcessorRevision + "<br>");
Response.Write("CPU類型" + cpuInfo.dwProcessorType + "<br>");
Response.Write("lpMaximumApplicationAddress" + cpuInfo.lpMaximumApplicationAddress + "<br>");
Response.Write("lpMinimumApplicationAddress" + cpuInfo.lpMinimumApplicationAddress + "<br>");
Response.Write("CPU類型:" + cpuInfo.dwProcessorType + "<br>");
Response.Write("可用交換文件大小:" + memoryInfo.dwAvailPageFile + "<br>");
Response.Write("可用物理內存大小:" + memoryInfo.dwAvailPhys + "<br>");
Response.Write("可用虛擬內存大小" + memoryInfo.dwAvailVirtual + "<br>");
Response.Write("操作系統位數:" + memoryInfo.dwLength + "<br>");
Response.Write("已經使用內存大小:" + memoryInfo.dwMemoryLoad + "<br>");
Response.Write("交換文件總大小:" + memoryInfo.dwTotalPageFile + "<br>");
Response.Write("總物理內存大小:" + memoryInfo.dwTotalPhys + "<br>");
Response.Write("總虛擬內存大小:" + memoryInfo.dwTotalVirtual + "<br>");
}
}
}
說明:前台aspx頁面沒有任何控件。