引言
在項(xiàng)目開發(fā)過程中,偶爾需要獲取計(jì)算機(jī)環(huán)境信息,如操作系統(tǒng)、部分硬件等。根據(jù)獲取的信息配置不同邏輯、提供硬件選擇(如打印機(jī)、攝像頭等)。本文將介紹幾個(gè)常用的獲取計(jì)算機(jī)或硬件信息方式。
系統(tǒng)
1、獲取操作系統(tǒng)信息
/// <summary>
/// 操作系統(tǒng)信息
/// </summary>
/// <returns></returns>
public void GetOperatingSystem(ref string operatingSystem,ref string osArchitecture)
{
try
{
using (ManagementObjectSearcher managementObjectSearcher = new ManagementObjectSearcher("select * from Win32_OperatingSystem"))
{
foreach (ManagementObject ManagementObject in managementObjectSearcher.Get())
{
operatingSystem = Convert.ToString(ManagementObject["Caption"]);
osArchitecture = Convert.ToString(ManagementObject["OSArchitecture"]);
break;
}
}
}
catch (Exception exception)
{
throw new Exception(exception.Message);
}
}
硬件
1、獲取打印機(jī)
#region 方式一 使用 ManagementObject 類獲取
/// <summary>
/// 獲取打印機(jī)
/// </summary>
public List<string> GetPrinters()
{
List<string> printerDevices = new List<string>();
try
{
using (var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Printer"))
{
foreach (var device in searcher.Get())
{
if (device["Name"] != null)
{
printerDevices.Add(Convert.ToString(device["Name"]));
}
}
}
}
catch (Exception exception)
{
throw new Exception(exception.Message);
}
return printerDevices;
}
#endregion、
#region 方式二 使用 Printing 獲取
/// <summary>
/// 獲取打印機(jī)
/// </summary>
public List<string> GetInstalledPrinters()
{
List<string> printerDevices = new List<string>();
try
{
foreach (string printerName in PrinterSettings.InstalledPrinters)
{
if (!printerDevices.Contains(printerName))
{
printerDevices.Add(printerName);
}
}
}
catch (Exception exception)
{
throw new Exception(exception.Message);
}
return printerDevices;
}
#endregion
2、獲取攝像頭
/// <summary>
/// 獲取攝像頭設(shè)備
/// </summary>
public List<string> GetCameras()
{
List<string> cameraDevices = new List<string>();
try
{
using (var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PnPEntity WHERE (PNPClass = 'Image' OR PNPClass = 'Camera')"))
{
foreach (var device in searcher.Get())
{
if (device["Caption"] != null)
{
cameraDevices.Add(Convert.ToString(device["Caption"]));
}
}
}
}
catch(Exception exception)
{
throw new Exception(exception.Message);
}
return cameraDevices;
}
3、獲取硬盤
/// <summary>
/// 硬盤信息
/// </summary>
public List<DiskDrive> GetDiskDrive()
{
List<DiskDrive> diskDevices = new List<DiskDrive>();
try
{
using (ManagementClass managementClass = new ManagementClass("Win32_DiskDrive"))
{
ManagementObjectCollection managementObjectCollection = managementClass.GetInstances();
foreach (ManagementObject managementObject in managementObjectCollection)
{
DiskDrive diskDrive = new DiskDrive();
diskDrive.SerialNumber = Convert.ToString(managementObject.Properties["SerialNumber"].Value);
diskDrive.SerialNumber = Convert.ToString(managementObject.Properties["Model"].Value);
diskDrive.SerialNumber = Convert.ToString(managementObject.Properties["Size"].Value);
diskDevices.Add(diskDrive);
}
}
}
catch (Exception exception)
{
throw new Exception(exception.Message);
}
return diskDevices;
}
4、獲取計(jì)算機(jī)主板
/// <summary>
/// 主板信息
/// </summary>
public List<BaseBoard> GetMainBoardInfo()
{
List<BaseBoard> baseBoardDevices = new List<BaseBoard>();
try
{
using (ManagementClass managementClass = new ManagementClass("Win32_BaseBoard"))
{
ManagementObjectCollection managementObjectCollection = managementClass.GetInstances();
foreach (ManagementObject managementObject in managementObjectCollection)
{
BaseBoard diskDrive = new BaseBoard();
diskDrive.SerialNumber = Convert.ToString(managementObject.Properties["SerialNumber"].Value);
diskDrive.Product = Convert.ToString(managementObject.Properties["Product"].Value);
diskDrive.Version = Convert.ToString(managementObject.Properties["Version"].Value);
diskDrive.Manufacturer = Convert.ToString(managementObject.Properties["Manufacturer"].Value);
baseBoardDevices.Add(diskDrive);
}
}
}
catch (Exception exception)
{
throw new Exception(exception.Message);
}
return baseBoardDevices;
}
5、獲取網(wǎng)絡(luò)地址
/// <summary>
/// 獲取網(wǎng)絡(luò)地址
/// </summary>
/// <returns></returns>
public List<string> GetIPAddress()
{
List<string> iPAddressList = new List<string>();
try
{
IPAddress[] allIPAddresses = Dns.GetHostAddresses(Dns.GetHostName());
foreach (IPAddress iPAddress in allIPAddresses)
{
if (iPAddress.AddressFamily.Equals(AddressFamily.InterNetwork))
{
// IP4
iPAddressList.Add(iPAddress.ToString());
continue;
}
}
}
catch (Exception exception)
{
throw new Exception(exception.Message);
}
return iPAddressList;
}
6、獲取物理地址
#region 方式一 使用 NetworkInterface 類獲取
public List<string> GetPhysicalAddress()
{
List<string> physicalAddressList = new List<string>();
try
{
NetworkInterface[] allNetworkInterface = NetworkInterface.GetAllNetworkInterfaces();
string result = string.Empty;
foreach (NetworkInterface networkInterface in allNetworkInterface)
{
result = networkInterface.GetPhysicalAddress().ToString();
result = result.Insert(2, ":");
result = result.Insert(5, ":");
result = result.Insert(8, ":");
result = result.Insert(11, ":");
result = result.Insert(14, ":");
physicalAddressList.Add(result);
}
}
catch (Exception exception)
{
throw new Exception(exception.Message);
}
return physicalAddressList;
}
#endregion
#region 方式二 使用 ManagementObject 類獲取
public static List<string> GetMacAddress()
{
List<string> physicalAddressList = new List<string>();
try
{
using (ManagementClass managementClass = new ManagementClass("Win32_NetworkAdapterConfiguration"))
{
ManagementObjectCollection managementObjectCollection = managementClass.GetInstances();
foreach (ManagementObject managementObject in managementObjectCollection)
{
if (managementObject["IPEnabled"] != null && managementObject["MacAddress"] != null)
{
if ("true".Equals(Convert.ToString(managementObject["IPEnabled"]),StringComparison.OrdinalIgnoreCase))
{
physicalAddressList.Add(Convert.ToString(managementObject["MacAddress"]));
}
}
}
}
}
catch (Exception exception)
{
throw new Exception(exception.Message);
}
return physicalAddressList;
}
7、獲取處理器信息
/// <summary>
/// 處理器信息
/// </summary>
/// <returns></returns>
public string GetCPUInfo()
{
StringBuilder stringBuilder = new StringBuilder();
try
{
using (ManagementObjectCollection.ManagementObjectEnumerator searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_Processor").Get().GetEnumerator())
{
if (searcher.MoveNext())
{
ManagementObject managementObject = (ManagementObject)searcher.Current;
stringBuilder.Append(managementObject.GetPropertyValue("Name").ToString());
stringBuilder.Append(",");
stringBuilder.Append(managementObject.GetPropertyValue("CurrentClockSpeed").ToString());
stringBuilder.Append(" Mhz,");
stringBuilder.Append(Environment.ProcessorCount.ToString());
stringBuilder.Append("核心");
}
}
}
catch (Exception exception)
{
throw new Exception(exception.Message);
}
return stringBuilder.ToString();
}
小結(jié)
以上是一些較常獲取計(jì)算機(jī)硬件信息或系統(tǒng)信息。還有很多信息可以使用C#獲取,這里不一一列舉。如有不到之處,請(qǐng)多多包涵。大家可以留言,提供一些你知道的方法。
該文章在 2024/6/8 18:09:26 編輯過