StableVersion4.3/HL_FrisAidPlat_Register_Client/ComputerInfo.cs

122 lines
4.3 KiB
C#
Raw Normal View History

2024-03-11 09:47:34 +08:00
using Microsoft.Win32;
using System;
using System.IO;
using System.Management;
using System.Net.NetworkInformation;
namespace HL_FrisAidPlat_Register
{
public class ComputerInfo
{
private static string ComputerInfofile = @"D:\ComputerInfo.key";
private static string RegistInfofile = @"D:\RegistInfo.key";
public static string GetComputerInfo()
{
string info = string.Empty;
string cpu = GetHardWareInfo("Win32_Processor", "ProcessorId");
string bios = GetHardWareInfo("Win32_BIOS", "SerialNumber");
string mac = GetHardWareInfo("Win32_BaseBoard", "SerialNumber");
info = string.Concat(cpu, bios,mac);
return info;
}
/// <summary>
/// 网络获取mac
/// </summary>
/// <returns></returns>
private static string GetMacAddressByNetworkInformation()
{
string key = "SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}\\";
string macAddress = string.Empty;
try
{
NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface adapter in nics)
{
if (adapter.NetworkInterfaceType == NetworkInterfaceType.Ethernet
&& adapter.GetPhysicalAddress().ToString().Length != 0)
{
string fRegistryKey = key + adapter.Id + "\\Connection";
RegistryKey rk = Registry.LocalMachine.OpenSubKey(fRegistryKey, false);
if (rk != null)
{
string fPnpInstanceID = rk.GetValue("PnpInstanceID", "").ToString();
int fMediaSubType = Convert.ToInt32(rk.GetValue("MediaSubType", 0));
if (fPnpInstanceID.Length > 3 &&
fPnpInstanceID.Substring(0, 3) == "PCI")
{
macAddress = adapter.GetPhysicalAddress().ToString();
for (int i = 1; i < 6; i++)
{
macAddress = macAddress.Insert(3 * i - 1, ":");
}
break;
}
}
}
}
}
catch (Exception)
{
}
return macAddress;
}
private static string GetHardWareInfo(string typePath, string key)
{
try
{
ManagementClass managementClass = new ManagementClass(typePath);
ManagementObjectCollection mn = managementClass.GetInstances();
PropertyDataCollection properties = managementClass.Properties;
foreach (PropertyData property in properties)
{
if (property.Name == key)
{
foreach (ManagementObject m in mn)
{
return m.Properties[property.Name].Value.ToString();
}
}
}
}
catch (Exception)
{
}
return string.Empty;
}
public static void WriteComputerInfoFile(string info)
{
WriteFile(info, ComputerInfofile);
}
public static void WriteRegistFile(string info)
{
WriteFile(info, RegistInfofile);
}
private static void WriteFile(string info, string fileName)
{
try
{
using (StreamWriter sw = new StreamWriter(fileName, false))
{
sw.Write(info);
sw.Close();
}
//FileInfo file = new FileInfo(ComputerInfofile);
//if (file.Exists)
//{ file.Attributes = FileAttributes.Hidden; }
//FileInfo file1 = new FileInfo(RegistInfofile);
//if (file1.Exists)
//{ file1.Attributes = FileAttributes.Hidden; }
}
catch (Exception)
{
}
}
}
}