|
@@ -36,6 +36,7 @@ namespace Masuit.Tools.Hardware
|
|
private static readonly string[] InstanceNames;
|
|
private static readonly string[] InstanceNames;
|
|
private static readonly PerformanceCounter[] NetRecvCounters;
|
|
private static readonly PerformanceCounter[] NetRecvCounters;
|
|
private static readonly PerformanceCounter[] NetSentCounters;
|
|
private static readonly PerformanceCounter[] NetSentCounters;
|
|
|
|
+ private static readonly Dictionary<string, dynamic> _cache = new();
|
|
|
|
|
|
#endregion 字段
|
|
#endregion 字段
|
|
|
|
|
|
@@ -94,17 +95,13 @@ namespace Masuit.Tools.Hardware
|
|
|
|
|
|
private static bool CompactFormat { get; set; }
|
|
private static bool CompactFormat { get; set; }
|
|
|
|
|
|
- #region CPU核心
|
|
|
|
|
|
+ #region CPU相关
|
|
|
|
|
|
/// <summary>
|
|
/// <summary>
|
|
/// 获取CPU核心数
|
|
/// 获取CPU核心数
|
|
/// </summary>
|
|
/// </summary>
|
|
public static int ProcessorCount { get; }
|
|
public static int ProcessorCount { get; }
|
|
|
|
|
|
- #endregion CPU核心
|
|
|
|
-
|
|
|
|
- #region CPU占用率
|
|
|
|
-
|
|
|
|
/// <summary>
|
|
/// <summary>
|
|
/// 获取CPU占用率 %
|
|
/// 获取CPU占用率 %
|
|
/// </summary>
|
|
/// </summary>
|
|
@@ -128,91 +125,44 @@ namespace Masuit.Tools.Hardware
|
|
return cpuUsedMs / (Environment.ProcessorCount * totalMsPassed) * 100;
|
|
return cpuUsedMs / (Environment.ProcessorCount * totalMsPassed) * 100;
|
|
}
|
|
}
|
|
|
|
|
|
- #endregion CPU占用率
|
|
|
|
-
|
|
|
|
- #region 可用内存
|
|
|
|
-
|
|
|
|
/// <summary>
|
|
/// <summary>
|
|
- /// 获取可用内存
|
|
|
|
|
|
+ /// WMI接口获取CPU使用率
|
|
/// </summary>
|
|
/// </summary>
|
|
- public static long MemoryAvailable
|
|
|
|
|
|
+ /// <returns></returns>
|
|
|
|
+ public static string GetProcessorData()
|
|
{
|
|
{
|
|
- get
|
|
|
|
- {
|
|
|
|
- try
|
|
|
|
- {
|
|
|
|
- using var mc = new ManagementClass("Win32_OperatingSystem");
|
|
|
|
- using var moc = mc.GetInstances();
|
|
|
|
- foreach (var mo in moc)
|
|
|
|
- {
|
|
|
|
- using (mo)
|
|
|
|
- {
|
|
|
|
- if (mo["FreePhysicalMemory"] != null)
|
|
|
|
- {
|
|
|
|
- return 1024 * long.Parse(mo["FreePhysicalMemory"].ToString());
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- return 0;
|
|
|
|
- }
|
|
|
|
- catch (Exception)
|
|
|
|
- {
|
|
|
|
- return 0;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
|
|
+ var d = GetCounterValue(CpuCounter, "Processor", "% Processor Time", "_Total");
|
|
|
|
+ return CompactFormat ? (int)d + "%" : d.ToString("F") + "%";
|
|
}
|
|
}
|
|
|
|
|
|
- #endregion 可用内存
|
|
|
|
-
|
|
|
|
- #region 物理内存
|
|
|
|
-
|
|
|
|
- /// <summary>
|
|
|
|
- /// 获取物理内存
|
|
|
|
- /// </summary>
|
|
|
|
- public static long PhysicalMemory { get; }
|
|
|
|
-
|
|
|
|
- #endregion 物理内存
|
|
|
|
-
|
|
|
|
- #region 查找所有应用程序标题
|
|
|
|
-
|
|
|
|
/// <summary>
|
|
/// <summary>
|
|
- /// 查找所有应用程序标题
|
|
|
|
|
|
+ /// 获取CPU温度
|
|
/// </summary>
|
|
/// </summary>
|
|
- /// <param name="handle">应用程序标题范型</param>
|
|
|
|
- /// <returns>所有应用程序集合</returns>
|
|
|
|
- public static ArrayList FindAllApps(int handle)
|
|
|
|
|
|
+ /// <returns>CPU温度</returns>
|
|
|
|
+ public static float GetCPUTemperature()
|
|
{
|
|
{
|
|
- var apps = new ArrayList();
|
|
|
|
- int hwCurr = GetWindow(handle, GwHwndfirst);
|
|
|
|
-
|
|
|
|
- while (hwCurr > 0)
|
|
|
|
|
|
+ try
|
|
{
|
|
{
|
|
- int IsTask = WsVisible | WsBorder;
|
|
|
|
- int lngStyle = GetWindowLongA(hwCurr, GwlStyle);
|
|
|
|
- bool taskWindow = (lngStyle & IsTask) == IsTask;
|
|
|
|
- if (taskWindow)
|
|
|
|
|
|
+ using var mos = new ManagementObjectSearcher(@"root\WMI", "select * from MSAcpi_ThermalZoneTemperature");
|
|
|
|
+ using var moc = mos.Get();
|
|
|
|
+ foreach (var mo in moc)
|
|
{
|
|
{
|
|
- int length = GetWindowTextLength(new IntPtr(hwCurr));
|
|
|
|
- StringBuilder sb = new StringBuilder(2 * length + 1);
|
|
|
|
- GetWindowText(hwCurr, sb, sb.Capacity);
|
|
|
|
- string strTitle = sb.ToString();
|
|
|
|
- if (!string.IsNullOrEmpty(strTitle))
|
|
|
|
|
|
+ using (mo)
|
|
{
|
|
{
|
|
- apps.Add(strTitle);
|
|
|
|
|
|
+ //这就是CPU的温度了
|
|
|
|
+ var temp = (float.Parse(mo.Properties["CurrentTemperature"].Value.ToString()) - 2732) / 10;
|
|
|
|
+ return (float)Math.Round(temp, 2);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
- hwCurr = GetWindow(hwCurr, GwHwndnext);
|
|
|
|
|
|
+ }
|
|
|
|
+ catch (Exception)
|
|
|
|
+ {
|
|
|
|
+ return 0;
|
|
}
|
|
}
|
|
|
|
|
|
- return apps;
|
|
|
|
|
|
+ return 0;
|
|
}
|
|
}
|
|
|
|
|
|
- #endregion 查找所有应用程序标题
|
|
|
|
-
|
|
|
|
- #region 获取CPU的数量
|
|
|
|
-
|
|
|
|
/// <summary>
|
|
/// <summary>
|
|
/// 获取CPU的数量
|
|
/// 获取CPU的数量
|
|
/// </summary>
|
|
/// </summary>
|
|
@@ -221,9 +171,12 @@ namespace Masuit.Tools.Hardware
|
|
{
|
|
{
|
|
try
|
|
try
|
|
{
|
|
{
|
|
- using var m = new ManagementClass("Win32_Processor");
|
|
|
|
- using var moc = m.GetInstances();
|
|
|
|
- return moc.Count;
|
|
|
|
|
|
+ return _cache.GetOrAdd(nameof(GetCpuCount), () =>
|
|
|
|
+ {
|
|
|
|
+ using var m = new ManagementClass("Win32_Processor");
|
|
|
|
+ using var moc = m.GetInstances();
|
|
|
|
+ return moc.Count;
|
|
|
|
+ });
|
|
}
|
|
}
|
|
catch (Exception)
|
|
catch (Exception)
|
|
{
|
|
{
|
|
@@ -231,16 +184,12 @@ namespace Masuit.Tools.Hardware
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- #endregion 获取CPU的数量
|
|
|
|
-
|
|
|
|
- #region 获取CPU信息
|
|
|
|
-
|
|
|
|
private static readonly Lazy<List<ManagementBaseObject>> CpuObjects = new Lazy<List<ManagementBaseObject>>(() =>
|
|
private static readonly Lazy<List<ManagementBaseObject>> CpuObjects = new Lazy<List<ManagementBaseObject>>(() =>
|
|
- {
|
|
|
|
- using var mos = new ManagementObjectSearcher("SELECT * FROM Win32_Processor");
|
|
|
|
- using var moc = mos.Get();
|
|
|
|
- return moc.AsParallel().Cast<ManagementBaseObject>().ToList();
|
|
|
|
- });
|
|
|
|
|
|
+ {
|
|
|
|
+ using var mos = new ManagementObjectSearcher("SELECT * FROM Win32_Processor");
|
|
|
|
+ using var moc = mos.Get();
|
|
|
|
+ return moc.AsParallel().Cast<ManagementBaseObject>().ToList();
|
|
|
|
+ });
|
|
|
|
|
|
/// <summary>
|
|
/// <summary>
|
|
/// 获取CPU信息
|
|
/// 获取CPU信息
|
|
@@ -252,7 +201,6 @@ namespace Masuit.Tools.Hardware
|
|
{
|
|
{
|
|
return CpuObjects.Value.Select(mo => new CpuInfo
|
|
return CpuObjects.Value.Select(mo => new CpuInfo
|
|
{
|
|
{
|
|
- CpuLoad = CpuLoad,
|
|
|
|
NumberOfLogicalProcessors = ProcessorCount,
|
|
NumberOfLogicalProcessors = ProcessorCount,
|
|
CurrentClockSpeed = mo.Properties["CurrentClockSpeed"].Value.ToString(),
|
|
CurrentClockSpeed = mo.Properties["CurrentClockSpeed"].Value.ToString(),
|
|
Manufacturer = mo.Properties["Manufacturer"].Value.ToString(),
|
|
Manufacturer = mo.Properties["Manufacturer"].Value.ToString(),
|
|
@@ -261,8 +209,7 @@ namespace Masuit.Tools.Hardware
|
|
DataWidth = mo.Properties["DataWidth"].Value.ToString(),
|
|
DataWidth = mo.Properties["DataWidth"].Value.ToString(),
|
|
SerialNumber = mo.Properties["ProcessorId"].Value.ToString(),
|
|
SerialNumber = mo.Properties["ProcessorId"].Value.ToString(),
|
|
DeviceID = mo.Properties["DeviceID"].Value.ToString(),
|
|
DeviceID = mo.Properties["DeviceID"].Value.ToString(),
|
|
- NumberOfCores = Convert.ToInt32(mo.Properties["NumberOfCores"].Value),
|
|
|
|
- Temperature = GetCPUTemperature()
|
|
|
|
|
|
+ NumberOfCores = Convert.ToInt32(mo.Properties["NumberOfCores"].Value)
|
|
}).ToList();
|
|
}).ToList();
|
|
}
|
|
}
|
|
catch (Exception)
|
|
catch (Exception)
|
|
@@ -271,77 +218,63 @@ namespace Masuit.Tools.Hardware
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- #endregion 获取CPU信息
|
|
|
|
-
|
|
|
|
- #region 获取内存信息
|
|
|
|
-
|
|
|
|
- /// <summary>
|
|
|
|
- /// 获取内存信息
|
|
|
|
- /// </summary>
|
|
|
|
- /// <returns>内存信息</returns>
|
|
|
|
- public static RamInfo GetRamInfo()
|
|
|
|
- {
|
|
|
|
- return new RamInfo
|
|
|
|
- {
|
|
|
|
- MemoryAvailable = GetFreePhysicalMemory(),
|
|
|
|
- PhysicalMemory = GetTotalPhysicalMemory(),
|
|
|
|
- TotalPageFile = GetTotalVirtualMemory(),
|
|
|
|
- AvailablePageFile = GetTotalVirtualMemory() - GetUsedVirtualMemory(),
|
|
|
|
- AvailableVirtual = 1 - GetUsageVirtualMemory(),
|
|
|
|
- TotalVirtual = 1 - GetUsedPhysicalMemory()
|
|
|
|
- };
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- #endregion 获取内存信息
|
|
|
|
|
|
+ #endregion CPU核心
|
|
|
|
|
|
- #region 获取CPU温度
|
|
|
|
|
|
+ #region 内存相关
|
|
|
|
|
|
/// <summary>
|
|
/// <summary>
|
|
- /// 获取CPU温度
|
|
|
|
|
|
+ /// 获取可用内存
|
|
/// </summary>
|
|
/// </summary>
|
|
- /// <returns>CPU温度</returns>
|
|
|
|
- public static float GetCPUTemperature()
|
|
|
|
|
|
+ public static long MemoryAvailable
|
|
{
|
|
{
|
|
- try
|
|
|
|
|
|
+ get
|
|
{
|
|
{
|
|
- using var mos = new ManagementObjectSearcher(@"root\WMI", "select * from MSAcpi_ThermalZoneTemperature");
|
|
|
|
- using var moc = mos.Get();
|
|
|
|
- foreach (var mo in moc)
|
|
|
|
|
|
+ try
|
|
{
|
|
{
|
|
- using (mo)
|
|
|
|
|
|
+ using var mc = new ManagementClass("Win32_OperatingSystem");
|
|
|
|
+ using var moc = mc.GetInstances();
|
|
|
|
+ foreach (var mo in moc)
|
|
{
|
|
{
|
|
- //这就是CPU的温度了
|
|
|
|
- var temp = (float.Parse(mo.Properties["CurrentTemperature"].Value.ToString()) - 2732) / 10;
|
|
|
|
- return (float)Math.Round(temp, 2);
|
|
|
|
|
|
+ using (mo)
|
|
|
|
+ {
|
|
|
|
+ if (mo["FreePhysicalMemory"] != null)
|
|
|
|
+ {
|
|
|
|
+ return 1024 * long.Parse(mo["FreePhysicalMemory"].ToString());
|
|
|
|
+ }
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ return 0;
|
|
|
|
+ }
|
|
|
|
+ catch (Exception)
|
|
|
|
+ {
|
|
|
|
+ return 0;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- catch (Exception)
|
|
|
|
- {
|
|
|
|
- return 0;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- return 0;
|
|
|
|
}
|
|
}
|
|
|
|
|
|
- #endregion 获取CPU温度
|
|
|
|
-
|
|
|
|
- #region WMI接口获取CPU使用率
|
|
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// 获取物理内存
|
|
|
|
+ /// </summary>
|
|
|
|
+ public static long PhysicalMemory { get; }
|
|
|
|
|
|
/// <summary>
|
|
/// <summary>
|
|
- /// WMI接口获取CPU使用率
|
|
|
|
|
|
+ /// 获取内存信息
|
|
/// </summary>
|
|
/// </summary>
|
|
- /// <returns></returns>
|
|
|
|
- public static string GetProcessorData()
|
|
|
|
|
|
+ /// <returns>内存信息</returns>
|
|
|
|
+ public static RamInfo GetRamInfo()
|
|
{
|
|
{
|
|
- float d = GetCounterValue(CpuCounter, "Processor", "% Processor Time", "_Total");
|
|
|
|
- return CompactFormat ? (int)d + "%" : d.ToString("F") + "%";
|
|
|
|
|
|
+ return new RamInfo
|
|
|
|
+ {
|
|
|
|
+ MemoryAvailable = GetFreePhysicalMemory(),
|
|
|
|
+ PhysicalMemory = GetTotalPhysicalMemory(),
|
|
|
|
+ TotalPageFile = GetTotalVirtualMemory(),
|
|
|
|
+ AvailablePageFile = GetTotalVirtualMemory() - GetUsedVirtualMemory(),
|
|
|
|
+ AvailableVirtual = 1 - GetUsageVirtualMemory(),
|
|
|
|
+ TotalVirtual = 1 - GetUsedPhysicalMemory()
|
|
|
|
+ };
|
|
}
|
|
}
|
|
|
|
|
|
- #endregion WMI接口获取CPU使用率
|
|
|
|
-
|
|
|
|
- #region 获取虚拟内存使用率详情
|
|
|
|
-
|
|
|
|
/// <summary>
|
|
/// <summary>
|
|
/// 获取虚拟内存使用率详情
|
|
/// 获取虚拟内存使用率详情
|
|
/// </summary>
|
|
/// </summary>
|
|
@@ -383,10 +316,6 @@ namespace Masuit.Tools.Hardware
|
|
return GetCounterValue(MemoryCounter, "Memory", "Commit Limit", null);
|
|
return GetCounterValue(MemoryCounter, "Memory", "Commit Limit", null);
|
|
}
|
|
}
|
|
|
|
|
|
- #endregion 获取虚拟内存使用率详情
|
|
|
|
-
|
|
|
|
- #region 获取物理内存使用率详情
|
|
|
|
-
|
|
|
|
/// <summary>
|
|
/// <summary>
|
|
/// 获取物理内存使用率详情描述
|
|
/// 获取物理内存使用率详情描述
|
|
/// </summary>
|
|
/// </summary>
|
|
@@ -409,8 +338,11 @@ namespace Masuit.Tools.Hardware
|
|
/// <returns></returns>
|
|
/// <returns></returns>
|
|
public static float GetTotalPhysicalMemory()
|
|
public static float GetTotalPhysicalMemory()
|
|
{
|
|
{
|
|
- string s = QueryComputerSystem("totalphysicalmemory");
|
|
|
|
- return s.TryConvertTo<float>();
|
|
|
|
|
|
+ return _cache.GetOrAdd(nameof(GetTotalPhysicalMemory), () =>
|
|
|
|
+ {
|
|
|
|
+ var s = QueryComputerSystem("totalphysicalmemory");
|
|
|
|
+ return s.TryConvertTo<float>();
|
|
|
|
+ });
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// <summary>
|
|
@@ -431,9 +363,9 @@ namespace Masuit.Tools.Hardware
|
|
return GetTotalPhysicalMemory() - GetFreePhysicalMemory();
|
|
return GetTotalPhysicalMemory() - GetFreePhysicalMemory();
|
|
}
|
|
}
|
|
|
|
|
|
- #endregion 获取物理内存使用率详情
|
|
|
|
|
|
+ #endregion 可用内存
|
|
|
|
|
|
- #region 获取硬盘的读写速率
|
|
|
|
|
|
+ #region 硬盘相关
|
|
|
|
|
|
/// <summary>
|
|
/// <summary>
|
|
/// 获取硬盘的读写速率
|
|
/// 获取硬盘的读写速率
|
|
@@ -442,9 +374,47 @@ namespace Masuit.Tools.Hardware
|
|
/// <returns></returns>
|
|
/// <returns></returns>
|
|
public static float GetDiskData(DiskData dd) => dd == DiskData.Read ? GetCounterValue(DiskReadCounter, "PhysicalDisk", "Disk Read Bytes/sec", "_Total") : dd == DiskData.Write ? GetCounterValue(DiskWriteCounter, "PhysicalDisk", "Disk Write Bytes/sec", "_Total") : dd == DiskData.ReadAndWrite ? GetCounterValue(DiskReadCounter, "PhysicalDisk", "Disk Read Bytes/sec", "_Total") + GetCounterValue(DiskWriteCounter, "PhysicalDisk", "Disk Write Bytes/sec", "_Total") : 0;
|
|
public static float GetDiskData(DiskData dd) => dd == DiskData.Read ? GetCounterValue(DiskReadCounter, "PhysicalDisk", "Disk Read Bytes/sec", "_Total") : dd == DiskData.Write ? GetCounterValue(DiskWriteCounter, "PhysicalDisk", "Disk Write Bytes/sec", "_Total") : dd == DiskData.ReadAndWrite ? GetCounterValue(DiskReadCounter, "PhysicalDisk", "Disk Read Bytes/sec", "_Total") + GetCounterValue(DiskWriteCounter, "PhysicalDisk", "Disk Write Bytes/sec", "_Total") : 0;
|
|
|
|
|
|
- #endregion 获取硬盘的读写速率
|
|
|
|
|
|
+ private static readonly List<DiskInfo> DiskInfos = new();
|
|
|
|
+
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// 获取磁盘可用空间
|
|
|
|
+ /// </summary>
|
|
|
|
+ /// <returns></returns>
|
|
|
|
+ public static List<DiskInfo> GetDiskInfo()
|
|
|
|
+ {
|
|
|
|
+ try
|
|
|
|
+ {
|
|
|
|
+ if (DiskInfos.Count > 0)
|
|
|
|
+ {
|
|
|
|
+ return DiskInfos;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ using var mc = new ManagementClass("Win32_DiskDrive");
|
|
|
|
+ using var moc = mc.GetInstances();
|
|
|
|
+ foreach (var mo in moc)
|
|
|
|
+ {
|
|
|
|
+ using (mo)
|
|
|
|
+ {
|
|
|
|
+ DiskInfos.Add(new DiskInfo()
|
|
|
|
+ {
|
|
|
|
+ Total = float.Parse(mo["Size"].ToString()),
|
|
|
|
+ Model = mo["Model"].ToString(),
|
|
|
|
+ SerialNumber = mo["SerialNumber"].ToString(),
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
|
|
- #region 获取网络的传输速率
|
|
|
|
|
|
+ return DiskInfos;
|
|
|
|
+ }
|
|
|
|
+ catch (Exception)
|
|
|
|
+ {
|
|
|
|
+ return new List<DiskInfo>();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ #endregion 硬盘相关
|
|
|
|
+
|
|
|
|
+ #region 网络相关
|
|
|
|
|
|
/// <summary>
|
|
/// <summary>
|
|
/// 获取网络的传输速率
|
|
/// 获取网络的传输速率
|
|
@@ -486,8 +456,6 @@ namespace Masuit.Tools.Hardware
|
|
return d;
|
|
return d;
|
|
}
|
|
}
|
|
|
|
|
|
- #endregion 获取网络的传输速率
|
|
|
|
-
|
|
|
|
/// <summary>
|
|
/// <summary>
|
|
/// 获取网卡硬件地址
|
|
/// 获取网卡硬件地址
|
|
/// </summary>
|
|
/// </summary>
|
|
@@ -497,21 +465,24 @@ namespace Masuit.Tools.Hardware
|
|
//获取网卡硬件地址
|
|
//获取网卡硬件地址
|
|
try
|
|
try
|
|
{
|
|
{
|
|
- IList<string> list = new List<string>();
|
|
|
|
- using var mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
|
|
|
|
- using var moc = mc.GetInstances();
|
|
|
|
- foreach (var mo in moc)
|
|
|
|
|
|
+ return _cache.GetOrAdd(nameof(GetMacAddress), () =>
|
|
{
|
|
{
|
|
- using (mo)
|
|
|
|
|
|
+ IList<string> list = new List<string>();
|
|
|
|
+ using var mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
|
|
|
|
+ using var moc = mc.GetInstances();
|
|
|
|
+ foreach (var mo in moc)
|
|
{
|
|
{
|
|
- if ((bool)mo["IPEnabled"])
|
|
|
|
|
|
+ using (mo)
|
|
{
|
|
{
|
|
- list.Add(mo["MacAddress"].ToString());
|
|
|
|
|
|
+ if ((bool)mo["IPEnabled"])
|
|
|
|
+ {
|
|
|
|
+ list.Add(mo["MacAddress"].ToString());
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- }
|
|
|
|
|
|
|
|
- return list;
|
|
|
|
|
|
+ return list;
|
|
|
|
+ });
|
|
}
|
|
}
|
|
catch (Exception)
|
|
catch (Exception)
|
|
{
|
|
{
|
|
@@ -528,6 +499,36 @@ namespace Masuit.Tools.Hardware
|
|
return GetLocalUsedIP(AddressFamily.InterNetwork);
|
|
return GetLocalUsedIP(AddressFamily.InterNetwork);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// 获取IP地址WMI
|
|
|
|
+ /// </summary>
|
|
|
|
+ /// <returns></returns>
|
|
|
|
+ public static string GetIPAddressWMI()
|
|
|
|
+ {
|
|
|
|
+ try
|
|
|
|
+ {
|
|
|
|
+ return _cache.GetOrAdd(nameof(GetIPAddressWMI), () =>
|
|
|
|
+ {
|
|
|
|
+ using var mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
|
|
|
|
+ using var moc = mc.GetInstances();
|
|
|
|
+ foreach (var mo in moc)
|
|
|
|
+ {
|
|
|
|
+ if ((bool)mo["IPEnabled"])
|
|
|
|
+ {
|
|
|
|
+ return ((string[])mo.Properties["IpAddress"].Value)[0];
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return "";
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+ catch (Exception e)
|
|
|
|
+ {
|
|
|
|
+ Console.WriteLine(e.Message);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return "";
|
|
|
|
+ }
|
|
|
|
+
|
|
/// <summary>
|
|
/// <summary>
|
|
/// 获取当前使用的IP
|
|
/// 获取当前使用的IP
|
|
/// </summary>
|
|
/// </summary>
|
|
@@ -547,29 +548,36 @@ namespace Masuit.Tools.Hardware
|
|
return interfaces.SelectMany(n => n.GetIPProperties().UnicastAddresses).ToList();
|
|
return interfaces.SelectMany(n => n.GetIPProperties().UnicastAddresses).ToList();
|
|
}
|
|
}
|
|
|
|
|
|
- #region 将速度值格式化成字节单位
|
|
|
|
-
|
|
|
|
/// <summary>
|
|
/// <summary>
|
|
- /// 将速度值格式化成字节单位
|
|
|
|
|
|
+ /// 获取网卡地址
|
|
/// </summary>
|
|
/// </summary>
|
|
- /// <param name="bytes"></param>
|
|
|
|
/// <returns></returns>
|
|
/// <returns></returns>
|
|
- public static string FormatBytes(this double bytes)
|
|
|
|
|
|
+ public static string GetNetworkCardAddress()
|
|
{
|
|
{
|
|
- int unit = 0;
|
|
|
|
- while (bytes > 1024)
|
|
|
|
|
|
+ try
|
|
{
|
|
{
|
|
- bytes /= 1024;
|
|
|
|
- ++unit;
|
|
|
|
|
|
+ return _cache.GetOrAdd(nameof(GetNetworkCardAddress), () =>
|
|
|
|
+ {
|
|
|
|
+ using var mos = new ManagementObjectSearcher("select * from Win32_NetworkAdapter where ((MACAddress Is Not NULL) and (Manufacturer <> 'Microsoft'))");
|
|
|
|
+ using var moc = mos.Get();
|
|
|
|
+ foreach (var mo in moc)
|
|
|
|
+ {
|
|
|
|
+ return mo["MACAddress"].ToString().Trim();
|
|
|
|
+ }
|
|
|
|
+ return "";
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+ catch (Exception e)
|
|
|
|
+ {
|
|
|
|
+ Console.WriteLine(e.Message);
|
|
}
|
|
}
|
|
|
|
|
|
- string s = CompactFormat ? ((int)bytes).ToString() : bytes.ToString("F") + " ";
|
|
|
|
- return s + (Unit)unit;
|
|
|
|
|
|
+ return "";
|
|
}
|
|
}
|
|
|
|
|
|
- #endregion 将速度值格式化成字节单位
|
|
|
|
|
|
+ #endregion 网络相关
|
|
|
|
|
|
- #region 查询计算机系统信息
|
|
|
|
|
|
+ #region 系统相关
|
|
|
|
|
|
/// <summary>
|
|
/// <summary>
|
|
/// 获取计算机开机时间
|
|
/// 获取计算机开机时间
|
|
@@ -617,60 +625,144 @@ namespace Masuit.Tools.Hardware
|
|
return string.Empty;
|
|
return string.Empty;
|
|
}
|
|
}
|
|
|
|
|
|
- #endregion 查询计算机系统信息
|
|
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// 查找所有应用程序标题
|
|
|
|
+ /// </summary>
|
|
|
|
+ /// <param name="handle">应用程序标题范型</param>
|
|
|
|
+ /// <returns>所有应用程序集合</returns>
|
|
|
|
+ public static ArrayList FindAllApps(int handle)
|
|
|
|
+ {
|
|
|
|
+ var apps = new ArrayList();
|
|
|
|
+ int hwCurr = GetWindow(handle, GwHwndfirst);
|
|
|
|
+
|
|
|
|
+ while (hwCurr > 0)
|
|
|
|
+ {
|
|
|
|
+ int IsTask = WsVisible | WsBorder;
|
|
|
|
+ int lngStyle = GetWindowLongA(hwCurr, GwlStyle);
|
|
|
|
+ bool taskWindow = (lngStyle & IsTask) == IsTask;
|
|
|
|
+ if (taskWindow)
|
|
|
|
+ {
|
|
|
|
+ int length = GetWindowTextLength(new IntPtr(hwCurr));
|
|
|
|
+ StringBuilder sb = new StringBuilder(2 * length + 1);
|
|
|
|
+ GetWindowText(hwCurr, sb, sb.Capacity);
|
|
|
|
+ string strTitle = sb.ToString();
|
|
|
|
+ if (!string.IsNullOrEmpty(strTitle))
|
|
|
|
+ {
|
|
|
|
+ apps.Add(strTitle);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
|
|
- #region 获取环境变量
|
|
|
|
|
|
+ hwCurr = GetWindow(hwCurr, GwHwndnext);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return apps;
|
|
|
|
+ }
|
|
|
|
|
|
/// <summary>
|
|
/// <summary>
|
|
- /// 获取环境变量
|
|
|
|
|
|
+ /// 操作系统类型
|
|
/// </summary>
|
|
/// </summary>
|
|
- /// <param name="type">环境变量名</param>
|
|
|
|
/// <returns></returns>
|
|
/// <returns></returns>
|
|
- public static string QueryEnvironment(string type) => Environment.ExpandEnvironmentVariables(type);
|
|
|
|
|
|
+ public static string GetSystemType()
|
|
|
|
+ {
|
|
|
|
+ try
|
|
|
|
+ {
|
|
|
|
+ return _cache.GetOrAdd(nameof(GetSystemType), () =>
|
|
|
|
+ {
|
|
|
|
+ using var mc = new ManagementClass("Win32_ComputerSystem");
|
|
|
|
+ using var moc = mc.GetInstances();
|
|
|
|
+ foreach (var mo in moc)
|
|
|
|
+ {
|
|
|
|
+ return mo["SystemType"].ToString().Trim();
|
|
|
|
+ }
|
|
|
|
+ return "";
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+ catch (Exception e)
|
|
|
|
+ {
|
|
|
|
+ Console.WriteLine(e.Message);
|
|
|
|
+ }
|
|
|
|
|
|
- #endregion 获取环境变量
|
|
|
|
|
|
+ return "";
|
|
|
|
+ }
|
|
|
|
|
|
- #region 获取磁盘空间
|
|
|
|
|
|
+ #endregion 系统相关
|
|
|
|
|
|
- private static readonly List<DiskInfo> DiskInfos = new();
|
|
|
|
|
|
+ #region 主板相关
|
|
|
|
|
|
/// <summary>
|
|
/// <summary>
|
|
- /// 获取磁盘可用空间
|
|
|
|
|
|
+ /// 获取主板序列号
|
|
/// </summary>
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
/// <returns></returns>
|
|
- public static List<DiskInfo> GetDiskInfo()
|
|
|
|
|
|
+ public static string GetBiosSerialNumber()
|
|
{
|
|
{
|
|
try
|
|
try
|
|
{
|
|
{
|
|
- if (DiskInfos.Count > 0)
|
|
|
|
- {
|
|
|
|
- return DiskInfos;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- using var mc = new ManagementClass("Win32_DiskDrive");
|
|
|
|
- using var moc = mc.GetInstances();
|
|
|
|
- foreach (var mo in moc)
|
|
|
|
|
|
+ return _cache.GetOrAdd(nameof(GetBiosSerialNumber), () =>
|
|
{
|
|
{
|
|
- using (mo)
|
|
|
|
|
|
+ using var searcher = new ManagementObjectSearcher("select * from Win32_BIOS");
|
|
|
|
+ using var mos = searcher.Get();
|
|
|
|
+ foreach (var mo in mos)
|
|
{
|
|
{
|
|
- DiskInfos.Add(new DiskInfo()
|
|
|
|
- {
|
|
|
|
- Total = float.Parse(mo["Size"].ToString()),
|
|
|
|
- Model = mo["Model"].ToString(),
|
|
|
|
- SerialNumber = mo["SerialNumber"].ToString(),
|
|
|
|
- });
|
|
|
|
|
|
+ return mo["SerialNumber"].ToString().Trim();
|
|
}
|
|
}
|
|
- }
|
|
|
|
|
|
+ return "";
|
|
|
|
+ });
|
|
|
|
|
|
- return DiskInfos;
|
|
|
|
}
|
|
}
|
|
- catch (Exception)
|
|
|
|
|
|
+ catch (Exception e)
|
|
{
|
|
{
|
|
- return new List<DiskInfo>();
|
|
|
|
|
|
+ Console.WriteLine(e.Message);
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ return "";
|
|
}
|
|
}
|
|
|
|
|
|
- #endregion 获取磁盘空间
|
|
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// 主板编号
|
|
|
|
+ /// </summary>
|
|
|
|
+ /// <returns></returns>
|
|
|
|
+ public static BiosInfo GetBiosInfo()
|
|
|
|
+ {
|
|
|
|
+ return _cache.GetOrAdd(nameof(GetBiosInfo), () =>
|
|
|
|
+ {
|
|
|
|
+ using var searcher = new ManagementObjectSearcher("select * from Win32_BaseBoard");
|
|
|
|
+ using var mos = searcher.Get();
|
|
|
|
+ foreach (var mo in mos)
|
|
|
|
+ {
|
|
|
|
+ return new BiosInfo
|
|
|
|
+ {
|
|
|
|
+ Manufacturer = mo.GetPropertyValue("Manufacturer").ToString(),
|
|
|
|
+ ID = mo["SerialNumber"].ToString(),
|
|
|
|
+ Model = mo["Product"].ToString(),
|
|
|
|
+ SerialNumber = GetBiosSerialNumber()
|
|
|
|
+ };
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return new BiosInfo();
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ #endregion
|
|
|
|
+
|
|
|
|
+ #region 公共函数
|
|
|
|
+
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// 将速度值格式化成字节单位
|
|
|
|
+ /// </summary>
|
|
|
|
+ /// <param name="bytes"></param>
|
|
|
|
+ /// <returns></returns>
|
|
|
|
+ public static string FormatBytes(this double bytes)
|
|
|
|
+ {
|
|
|
|
+ int unit = 0;
|
|
|
|
+ while (bytes > 1024)
|
|
|
|
+ {
|
|
|
|
+ bytes /= 1024;
|
|
|
|
+ ++unit;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ string s = CompactFormat ? ((int)bytes).ToString() : bytes.ToString("F") + " ";
|
|
|
|
+ return s + (Unit)unit;
|
|
|
|
+ }
|
|
|
|
|
|
private static float GetCounterValue(PerformanceCounter pc, string categoryName, string counterName, string instanceName)
|
|
private static float GetCounterValue(PerformanceCounter pc, string categoryName, string counterName, string instanceName)
|
|
{
|
|
{
|
|
@@ -680,6 +772,8 @@ namespace Masuit.Tools.Hardware
|
|
return pc.NextValue();
|
|
return pc.NextValue();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ #endregion 公共函数
|
|
|
|
+
|
|
#region Win32API声明
|
|
#region Win32API声明
|
|
|
|
|
|
#pragma warning disable 1591
|
|
#pragma warning disable 1591
|