初始化上传
This commit is contained in:
39
常用工具集/Utility/CZGL.SystemInfo/CPU/CPUHelper.cs
Normal file
39
常用工具集/Utility/CZGL.SystemInfo/CPU/CPUHelper.cs
Normal file
@@ -0,0 +1,39 @@
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace CZGL.SystemInfo
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public static class CPUHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取当前系统消耗的 CPU 时间
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static CPUTime GetCPUTime()
|
||||
{
|
||||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
||||
return WindowsCPU.GetCPUTime();
|
||||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
|
||||
return LinuxCPU.GetCPUTime();
|
||||
return new CPUTime();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 计算 CPU 使用率
|
||||
/// </summary>
|
||||
/// <param name="oldTime"></param>
|
||||
/// <param name="newTime"></param>
|
||||
/// <returns></returns>
|
||||
public static double CalculateCPULoad(CPUTime oldTime, CPUTime newTime)
|
||||
{
|
||||
ulong totalTicksSinceLastTime = newTime.SystemTime - oldTime.SystemTime;
|
||||
ulong idleTicksSinceLastTime = newTime.IdleTime - oldTime.IdleTime;
|
||||
|
||||
double ret = 1.0f - ((totalTicksSinceLastTime > 0) ? ((double)idleTicksSinceLastTime) / totalTicksSinceLastTime : 0);
|
||||
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
}
|
||||
29
常用工具集/Utility/CZGL.SystemInfo/CPU/CPUTime.cs
Normal file
29
常用工具集/Utility/CZGL.SystemInfo/CPU/CPUTime.cs
Normal file
@@ -0,0 +1,29 @@
|
||||
namespace CZGL.SystemInfo
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public struct CPUTime
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="idleTime"></param>
|
||||
/// <param name="systemTime"></param>
|
||||
public CPUTime(ulong idleTime, ulong systemTime)
|
||||
{
|
||||
IdleTime = idleTime;
|
||||
SystemTime = systemTime;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// CPU 空闲时间
|
||||
/// </summary>
|
||||
public ulong IdleTime { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// CPU 工作时间
|
||||
/// </summary>
|
||||
public ulong SystemTime { get; private set; }
|
||||
}
|
||||
}
|
||||
21
常用工具集/Utility/CZGL.SystemInfo/CPU/FILETIME.cs
Normal file
21
常用工具集/Utility/CZGL.SystemInfo/CPU/FILETIME.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace CZGL.SystemInfo
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct FILETIME
|
||||
{
|
||||
/// <summary>
|
||||
/// 时间的低位部分
|
||||
/// </summary>
|
||||
public uint DateTimeLow;
|
||||
|
||||
/// <summary>
|
||||
/// 时间的高位部分
|
||||
/// </summary>
|
||||
public uint DateTimeHigh;
|
||||
}
|
||||
}
|
||||
55
常用工具集/Utility/CZGL.SystemInfo/CPU/LinuxCPU.cs
Normal file
55
常用工具集/Utility/CZGL.SystemInfo/CPU/LinuxCPU.cs
Normal file
@@ -0,0 +1,55 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace CZGL.SystemInfo
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// Linux
|
||||
/// </summary>
|
||||
public static class LinuxCPU
|
||||
{
|
||||
const string Path = "/proc/stat";
|
||||
|
||||
/// <summary>
|
||||
/// 获取 CPU 时间
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static CPUTime GetCPUTime()
|
||||
{
|
||||
ulong IdleTime = 0;
|
||||
ulong SystemTime = 0;
|
||||
try
|
||||
{
|
||||
var text = File.ReadAllLines(Path);
|
||||
|
||||
foreach (var item in text)
|
||||
{
|
||||
if (!item.StartsWith("cpu")) continue;
|
||||
#if NET6_0_OR_GREATER
|
||||
var values = item.Split(" ", StringSplitOptions.RemoveEmptyEntries).ToArray();
|
||||
SystemTime += (ulong)(values[1..].Select(x => decimal.Parse(x)).Sum());
|
||||
#else
|
||||
var values = item.Split(new char[] { ' '}, StringSplitOptions.RemoveEmptyEntries).ToArray();
|
||||
SystemTime += (ulong)(values.ToList().GetRange(1, values.Length).Select(x => decimal.Parse(x)).Sum());
|
||||
#endif
|
||||
|
||||
IdleTime += ulong.Parse(values[4]);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.WriteLine(ex.ToString());
|
||||
Debug.Assert(false, ex.Message);
|
||||
throw new PlatformNotSupportedException($"{RuntimeInformation.OSArchitecture.ToString()} {Environment.OSVersion.Platform.ToString()} {Environment.OSVersion.ToString()}");
|
||||
}
|
||||
|
||||
return new CPUTime(IdleTime, SystemTime);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
71
常用工具集/Utility/CZGL.SystemInfo/CPU/WindowsCPU.cs
Normal file
71
常用工具集/Utility/CZGL.SystemInfo/CPU/WindowsCPU.cs
Normal file
@@ -0,0 +1,71 @@
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace CZGL.SystemInfo
|
||||
{
|
||||
/// <summary>
|
||||
/// Windows
|
||||
/// </summary>
|
||||
public partial class WindowsCPU
|
||||
{
|
||||
/*
|
||||
IdleTime 空闲时间
|
||||
KernelTime 内核时间
|
||||
UserTime 用户时间
|
||||
|
||||
系统时间 = 内核时间 + 用户时间
|
||||
SystemTime = KernelTime + UserTime
|
||||
|
||||
*/
|
||||
|
||||
/// <summary>
|
||||
/// 在多处理器系统上,返回的值是所有处理器指定时间的总和
|
||||
/// </summary>
|
||||
/// <remarks><see href="https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getsystemtimes"/></remarks>
|
||||
/// <param name="lpIdleTime">指向 FILETIME 结构的指针,该结构接收系统空闲的时间量</param>
|
||||
/// <param name="lpKernelTime">指向 FILETIME 结构的指针,该结构接收系统在内核模式下执行的时间量(包括所有进程中的所有线程以及所有处理器上的所有线程)。此时间值还包括系统空闲的时间</param>
|
||||
/// <param name="lpUserTime">指向 FILETIME 结构的指针,该结构接收系统在 User 模式下执行的时间量(包括所有进程中的所有线程以及所有处理器上的所有线程)</param>
|
||||
/// <returns></returns>
|
||||
#if NET7_0_OR_GREATER
|
||||
[LibraryImport("kernel32.dll", SetLastError = true)]
|
||||
[return: MarshalAs(UnmanagedType.Bool)]
|
||||
public static partial bool GetSystemTimes(out FILETIME lpIdleTime, out FILETIME lpKernelTime, out FILETIME lpUserTime);
|
||||
#else
|
||||
[DllImport("kernel32.dll", SetLastError = true)]
|
||||
[return: MarshalAs(UnmanagedType.Bool)]
|
||||
public static extern bool GetSystemTimes(out FILETIME lpIdleTime, out FILETIME lpKernelTime, out FILETIME lpUserTime);
|
||||
#endif
|
||||
/// <summary>
|
||||
/// 获取 CPU 工作时间
|
||||
/// </summary>
|
||||
/// <param name="lpIdleTime"></param>
|
||||
/// <param name="lpKernelTime"></param>
|
||||
/// <param name="lpUserTime"></param>
|
||||
/// <returns></returns>
|
||||
public static CPUTime GetCPUTime(FILETIME lpIdleTime, FILETIME lpKernelTime, FILETIME lpUserTime)
|
||||
{
|
||||
var IdleTime = ((ulong)lpIdleTime.DateTimeHigh << 32) | lpIdleTime.DateTimeLow;
|
||||
var KernelTime = ((ulong)lpKernelTime.DateTimeHigh << 32) | lpKernelTime.DateTimeLow;
|
||||
var UserTime = ((ulong)lpUserTime.DateTimeHigh << 32) | lpUserTime.DateTimeLow;
|
||||
|
||||
var SystemTime = KernelTime + UserTime;
|
||||
|
||||
return new CPUTime(IdleTime, SystemTime);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取 CPU 工作时间
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static CPUTime GetCPUTime()
|
||||
{
|
||||
FILETIME lpIdleTime = default;
|
||||
FILETIME lpKernelTime = default;
|
||||
FILETIME lpUserTime = default;
|
||||
if (!GetSystemTimes(out lpIdleTime, out lpKernelTime, out lpUserTime))
|
||||
{
|
||||
return default;
|
||||
}
|
||||
return GetCPUTime(lpIdleTime, lpKernelTime, lpUserTime);
|
||||
}
|
||||
}
|
||||
}
|
||||
129
常用工具集/Utility/CZGL.SystemInfo/Disk/DiskInfo.cs
Normal file
129
常用工具集/Utility/CZGL.SystemInfo/Disk/DiskInfo.cs
Normal file
@@ -0,0 +1,129 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
|
||||
namespace CZGL.SystemInfo
|
||||
{
|
||||
/// <summary>
|
||||
/// 磁盘信息
|
||||
/// </summary>
|
||||
public class DiskInfo
|
||||
{
|
||||
private readonly DriveInfo _info;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 获取磁盘类
|
||||
/// </summary>
|
||||
public DriveInfo DriveInfo => _info;
|
||||
|
||||
private DiskInfo(DriveInfo info)
|
||||
{
|
||||
_info = info;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 驱动器名称
|
||||
/// <para>ex: C:\</para>
|
||||
/// </summary>
|
||||
public string Id => _info.Name;
|
||||
|
||||
/// <summary>
|
||||
/// 磁盘名称
|
||||
/// <para>ex:<br />
|
||||
/// Windows: system<br />
|
||||
/// Linux: /dev
|
||||
/// </para>
|
||||
/// </summary>
|
||||
public string Name => _info.Name;
|
||||
|
||||
/// <summary>
|
||||
/// 获取驱动器类型
|
||||
/// </summary>
|
||||
/// <remarks>获取驱动器类型,如 CD-ROM、可移动、网络或固定</remarks>
|
||||
public DriveType DriveType => _info.DriveType;
|
||||
|
||||
/// <summary>
|
||||
/// 文件系统
|
||||
/// <para>
|
||||
/// Windows: NTFS、 CDFS...<br />
|
||||
/// Linux: rootfs、tmpfs、binfmt_misc...
|
||||
/// </para>
|
||||
/// </summary>
|
||||
public string FileSystem => _info.DriveFormat;
|
||||
|
||||
/// <summary>
|
||||
/// 磁盘剩余容量(以字节为单位)
|
||||
/// </summary>
|
||||
public long FreeSpace => _info.AvailableFreeSpace;
|
||||
|
||||
/// <summary>
|
||||
/// 磁盘总容量(以字节为单位)
|
||||
/// </summary>
|
||||
public long TotalSize => _info.TotalSize;
|
||||
|
||||
/// <summary>
|
||||
/// 磁盘剩余可用容量
|
||||
/// </summary>
|
||||
public long UsedSize => TotalSize - FreeSpace;
|
||||
|
||||
/// <summary>
|
||||
/// 磁盘根目录位置
|
||||
/// </summary>
|
||||
public string RootPath => _info.RootDirectory.FullName;
|
||||
|
||||
/// <summary>
|
||||
/// 获取本地所有磁盘信息
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static DiskInfo[] GetDisks()
|
||||
{
|
||||
return DriveInfo.GetDrives().Where(x => x.IsReady).Select(x => new DiskInfo(x)).ToArray();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取 Docker 运行的容器其容器文件系统在主机中的存储位置
|
||||
/// </summary>
|
||||
/// <remarks>程序需要在宿主机运行才有效果,在容器中运行,调用此API获取不到相关信息</remarks>
|
||||
/// <returns></returns>
|
||||
public static DiskInfo[] GetDockerMerge()
|
||||
{
|
||||
return DriveInfo.GetDrives()
|
||||
.Where(x => x.DriveFormat.Equals("overlay", StringComparison.OrdinalIgnoreCase) && x.DriveFormat.Contains("docker"))
|
||||
.Select(x => new DiskInfo(x)).ToArray();
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 筛选出真正能够使用的磁盘
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static DiskInfo[] GetRealDisk()
|
||||
{
|
||||
var disks = DriveInfo.GetDrives()
|
||||
.Where(x =>
|
||||
x.DriveType == DriveType.Fixed &&
|
||||
x.TotalSize != 0 && x.DriveFormat != "overlay");
|
||||
|
||||
return disks.Select(x => new DiskInfo(x))
|
||||
.Distinct(new DiskInfoEquality()).ToArray();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 筛选重复项
|
||||
/// </summary>
|
||||
private class DiskInfoEquality : IEqualityComparer<DiskInfo>
|
||||
{
|
||||
public bool Equals(DiskInfo x, DiskInfo y)
|
||||
{
|
||||
return x?.Id == y?.Id;
|
||||
}
|
||||
|
||||
public int GetHashCode(DiskInfo obj)
|
||||
{
|
||||
return obj.Id.GetHashCode();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
54
常用工具集/Utility/CZGL.SystemInfo/Memory/LinuxMemory.cs
Normal file
54
常用工具集/Utility/CZGL.SystemInfo/Memory/LinuxMemory.cs
Normal file
@@ -0,0 +1,54 @@
|
||||
using CZGL.SystemInfo.Memory;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace CZGL.SystemInfo
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public partial class LinuxMemory
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static MemoryValue GetMemory()
|
||||
{
|
||||
Sysinfo info = new Sysinfo();
|
||||
if (sysinfo(ref info) != 0)
|
||||
{
|
||||
return default;
|
||||
}
|
||||
var usedPercentage = (((double)info.totalram - info.freeram) / (double)info.totalram) * 100;
|
||||
MemoryValue value = new MemoryValue(info.totalram, info.freeram, (ulong)usedPercentage, info.totalswap, info.freeswap);
|
||||
return value;
|
||||
}
|
||||
|
||||
#if NET7_0_OR_GREATER
|
||||
|
||||
/// <summary>
|
||||
/// 返回整个系统统计信息,<see href="https://linux.die.net/man/2/sysinfo"/>
|
||||
/// </summary>
|
||||
/// <remarks>int sysinfo(struct sysinfo *info);</remarks>
|
||||
/// <param name="info"></param>
|
||||
/// <returns></returns>
|
||||
[LibraryImport("libc.so.6", SetLastError = true)]
|
||||
[return: MarshalAs(UnmanagedType.I4)]
|
||||
public static partial System.Int32 sysinfo(ref Sysinfo info);
|
||||
|
||||
#else
|
||||
|
||||
/// <summary>
|
||||
/// 返回整个系统统计信息,<see href="https://linux.die.net/man/2/sysinfo"/>
|
||||
/// </summary>
|
||||
/// <remarks>int sysinfo(struct sysinfo *info);</remarks>
|
||||
/// <param name="info"></param>
|
||||
/// <returns></returns>
|
||||
[DllImport("libc.so.6", CharSet = CharSet.Auto, SetLastError = true)]
|
||||
[return: MarshalAs(UnmanagedType.I4)]
|
||||
public static extern System.Int32 sysinfo(ref Sysinfo info);
|
||||
|
||||
#endif
|
||||
}
|
||||
}
|
||||
17
常用工具集/Utility/CZGL.SystemInfo/Memory/MEMORYSTATUS.cs
Normal file
17
常用工具集/Utility/CZGL.SystemInfo/Memory/MEMORYSTATUS.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
namespace CZGL.SystemInfo
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public struct MEMORYSTATUS
|
||||
{
|
||||
public uint dwLength;
|
||||
public uint dwMemoryLoad;
|
||||
public uint dwTotalPhys;
|
||||
public uint dwAvailPhys;
|
||||
public uint dwTotalPageFile;
|
||||
public uint dwAvailPageFile;
|
||||
public uint dwTotalVirtual;
|
||||
public uint dwAvailVirtual;
|
||||
}
|
||||
}
|
||||
24
常用工具集/Utility/CZGL.SystemInfo/Memory/MemoryHelper.cs
Normal file
24
常用工具集/Utility/CZGL.SystemInfo/Memory/MemoryHelper.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace CZGL.SystemInfo
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public static class MemoryHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取当前系统的内存信息
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static MemoryValue GetMemoryValue()
|
||||
{
|
||||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
||||
return WindowsMemory.GetMemory();
|
||||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
|
||||
return LinuxMemory.GetMemory();
|
||||
|
||||
return default;
|
||||
}
|
||||
}
|
||||
}
|
||||
68
常用工具集/Utility/CZGL.SystemInfo/Memory/MemoryStatusExE.cs
Normal file
68
常用工具集/Utility/CZGL.SystemInfo/Memory/MemoryStatusExE.cs
Normal file
@@ -0,0 +1,68 @@
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace CZGL.SystemInfo.Memory
|
||||
{
|
||||
/// <summary>
|
||||
/// 包含有关物理内存和虚拟内存(包括扩展内存)的当前状态的信息。该 GlobalMemoryStatusEx在这个构造函数存储信息。
|
||||
/// <see ref="https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/ns-sysinfoapi-memorystatusex"/>
|
||||
/// </summary>
|
||||
public struct MemoryStatusExE
|
||||
{
|
||||
/// <summary>
|
||||
/// 结构的大小,以字节为单位,必须在调用 GlobalMemoryStatusEx 之前设置此成员,可以用 Init 方法提前处理
|
||||
/// </summary>
|
||||
/// <remarks>应当使用本对象提供的 Init ,而不是使用构造函数!</remarks>
|
||||
public uint dwLength;
|
||||
|
||||
/// <summary>
|
||||
/// 一个介于 0 和 100 之间的数字,用于指定正在使用的物理内存的大致百分比(0 表示没有内存使用,100 表示内存已满)。
|
||||
/// </summary>
|
||||
public uint dwMemoryLoad;
|
||||
|
||||
/// <summary>
|
||||
/// 实际物理内存量,以字节为单位
|
||||
/// </summary>
|
||||
public ulong ullTotalPhys;
|
||||
|
||||
/// <summary>
|
||||
/// 当前可用的物理内存量,以字节为单位。这是可以立即重用而无需先将其内容写入磁盘的物理内存量。它是备用列表、空闲列表和零列表的大小之和
|
||||
/// </summary>
|
||||
public ulong ullAvailPhys;
|
||||
|
||||
/// <summary>
|
||||
/// 系统或当前进程的当前已提交内存限制,以字节为单位,以较小者为准。要获得系统范围的承诺内存限制,请调用GetPerformanceInfo
|
||||
/// </summary>
|
||||
public ulong ullTotalPageFile;
|
||||
|
||||
/// <summary>
|
||||
/// 当前进程可以提交的最大内存量,以字节为单位。该值等于或小于系统范围的可用提交值。要计算整个系统的可承诺值,调用GetPerformanceInfo核减价值CommitTotal从价值CommitLimit
|
||||
/// </summary>
|
||||
|
||||
public ulong ullAvailPageFile;
|
||||
|
||||
/// <summary>
|
||||
/// 调用进程的虚拟地址空间的用户模式部分的大小,以字节为单位。该值取决于进程类型、处理器类型和操作系统的配置。例如,对于 x86 处理器上的大多数 32 位进程,此值约为 2 GB,对于在启用4 GB 调整的系统上运行的具有大地址感知能力的 32 位进程约为 3 GB 。
|
||||
/// </summary>
|
||||
|
||||
public ulong ullTotalVirtual;
|
||||
|
||||
/// <summary>
|
||||
/// 当前在调用进程的虚拟地址空间的用户模式部分中未保留和未提交的内存量,以字节为单位
|
||||
/// </summary>
|
||||
public ulong ullAvailVirtual;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 预订的。该值始终为 0
|
||||
/// </summary>
|
||||
public ulong ullAvailExtendedVirtual;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public void Init()
|
||||
{
|
||||
dwLength = checked((uint)Marshal.SizeOf(typeof(MemoryStatusExE)));
|
||||
}
|
||||
}
|
||||
}
|
||||
66
常用工具集/Utility/CZGL.SystemInfo/Memory/MemoryValue.cs
Normal file
66
常用工具集/Utility/CZGL.SystemInfo/Memory/MemoryValue.cs
Normal file
@@ -0,0 +1,66 @@
|
||||
namespace CZGL.SystemInfo
|
||||
{
|
||||
/// <summary>
|
||||
/// 内存值表示
|
||||
/// </summary>
|
||||
public struct MemoryValue
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="totalPhysicalMemory">物理内存字节数</param>
|
||||
/// <param name="availablePhysicalMemory">可用的物理内存字节数</param>
|
||||
/// <param name="usedPercentage">已用物理内存百分比</param>
|
||||
/// <param name="totalVirtualMemory">虚拟内存字节数</param>
|
||||
/// <param name="availableVirtualMemory">可用虚拟内存字节数</param>
|
||||
public MemoryValue(
|
||||
ulong totalPhysicalMemory,
|
||||
ulong availablePhysicalMemory,
|
||||
double usedPercentage,
|
||||
ulong totalVirtualMemory,
|
||||
ulong availableVirtualMemory)
|
||||
{
|
||||
TotalPhysicalMemory = totalPhysicalMemory;
|
||||
AvailablePhysicalMemory = availablePhysicalMemory;
|
||||
UsedPercentage = usedPercentage;
|
||||
TotalVirtualMemory = totalVirtualMemory;
|
||||
AvailableVirtualMemory = availableVirtualMemory;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 物理内存字节数
|
||||
/// </summary>
|
||||
public ulong TotalPhysicalMemory { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// 可用的物理内存字节数
|
||||
/// </summary>
|
||||
public ulong AvailablePhysicalMemory { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// 已用物理内存字节数
|
||||
/// </summary>
|
||||
public ulong UsedPhysicalMemory => TotalPhysicalMemory - AvailablePhysicalMemory;
|
||||
|
||||
/// <summary>
|
||||
/// 已用物理内存百分比,0~100,100表示内存已用尽
|
||||
/// </summary>
|
||||
public double UsedPercentage { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// 虚拟内存字节数
|
||||
/// </summary>
|
||||
public ulong TotalVirtualMemory { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// 可用虚拟内存字节数
|
||||
/// </summary>
|
||||
public ulong AvailableVirtualMemory { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// 已用虚拟内存字节数
|
||||
/// </summary>
|
||||
public ulong UsedVirtualMemory => TotalVirtualMemory - AvailableVirtualMemory;
|
||||
}
|
||||
|
||||
}
|
||||
74
常用工具集/Utility/CZGL.SystemInfo/Memory/Sysinfo.cs
Normal file
74
常用工具集/Utility/CZGL.SystemInfo/Memory/Sysinfo.cs
Normal file
@@ -0,0 +1,74 @@
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace CZGL.SystemInfo
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public struct Sysinfo
|
||||
{
|
||||
/// <summary>
|
||||
/// Seconds since boot
|
||||
/// </summary>
|
||||
public long uptime;
|
||||
|
||||
/// <summary>
|
||||
/// 获取 1,5,15 分钟内存的平均使用量,数组大小为 3
|
||||
/// </summary>
|
||||
unsafe public fixed ulong loads[3];
|
||||
/// <summary>
|
||||
/// 总物理内存
|
||||
/// </summary>
|
||||
public ulong totalram;
|
||||
|
||||
/// <summary>
|
||||
/// 可用内存
|
||||
/// </summary>
|
||||
public ulong freeram;
|
||||
|
||||
/// <summary>
|
||||
/// 共享内存
|
||||
/// </summary>
|
||||
public ulong sharedram;
|
||||
|
||||
/// <summary>
|
||||
/// Memory used by buffers
|
||||
/// </summary>
|
||||
public ulong bufferram;
|
||||
|
||||
/// <summary>
|
||||
/// Total swap space size
|
||||
/// </summary>
|
||||
public ulong totalswap;
|
||||
|
||||
/// <summary>
|
||||
/// swap space still available
|
||||
/// </summary>
|
||||
public ulong freeswap;
|
||||
|
||||
/// <summary>
|
||||
/// Number of current processes
|
||||
/// </summary>
|
||||
public ushort procs;
|
||||
|
||||
/// <summary>
|
||||
/// Total high memory size
|
||||
/// </summary>
|
||||
public ulong totalhigh;
|
||||
|
||||
/// <summary>
|
||||
/// Available high memory size
|
||||
/// </summary>
|
||||
public ulong freehigh;
|
||||
|
||||
/// <summary>
|
||||
/// Memory unit size in bytes
|
||||
/// </summary>
|
||||
public uint mem_unit;
|
||||
|
||||
/// <summary>
|
||||
/// Padding to 64 bytes
|
||||
/// </summary>
|
||||
unsafe public fixed byte _f[64];
|
||||
}
|
||||
}
|
||||
86
常用工具集/Utility/CZGL.SystemInfo/Memory/WindowsMemory.cs
Normal file
86
常用工具集/Utility/CZGL.SystemInfo/Memory/WindowsMemory.cs
Normal file
@@ -0,0 +1,86 @@
|
||||
using CZGL.SystemInfo.Memory;
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace CZGL.SystemInfo
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public partial class WindowsMemory
|
||||
{
|
||||
|
||||
#if NET7_0_OR_GREATER
|
||||
|
||||
/// <summary>
|
||||
/// 在内存超过 4 GB 的计算机上, GlobalMemoryStatus函数可能返回不正确的信息,报告值 –1 表示溢出。因此,应用程序应改用 GlobalMemoryStatusEx函数。
|
||||
/// </summary>
|
||||
/// <remarks>Windows XP [仅限桌面应用程序];最低支持服务器 Windows Server 2003 [仅限桌面应用程序]</remarks>
|
||||
/// <param name="lpBuffer"></param>
|
||||
[LibraryImport("Kernel32.dll", SetLastError = true)]
|
||||
public static partial void GlobalMemoryStatus(ref MEMORYSTATUS lpBuffer);
|
||||
|
||||
/// <summary>
|
||||
/// 检索有关系统当前使用物理和虚拟内存的信息
|
||||
/// </summary>
|
||||
/// <remarks><see href="https://docs.microsoft.com/zh-cn/windows/win32/api/sysinfoapi/nf-sysinfoapi-globalmemorystatusex"/></remarks>
|
||||
/// <param name="lpBuffer"></param>
|
||||
/// <returns></returns>
|
||||
[LibraryImport("Kernel32.dll", SetLastError = true)]
|
||||
[return: MarshalAs(UnmanagedType.Bool)]
|
||||
public static partial Boolean GlobalMemoryStatusEx(ref MemoryStatusExE lpBuffer);
|
||||
|
||||
#else
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 在内存超过 4 GB 的计算机上, GlobalMemoryStatus函数可能返回不正确的信息,报告值 –1 表示溢出。因此,应用程序应改用 GlobalMemoryStatusEx函数。
|
||||
/// </summary>
|
||||
/// <remarks>Windows XP [仅限桌面应用程序];最低支持服务器 Windows Server 2003 [仅限桌面应用程序]</remarks>
|
||||
/// <param name="lpBuffer"></param>
|
||||
[DllImport("Kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
|
||||
public static extern void GlobalMemoryStatus(ref MEMORYSTATUS lpBuffer);
|
||||
|
||||
/// <summary>
|
||||
/// 检索有关系统当前使用物理和虚拟内存的信息
|
||||
/// </summary>
|
||||
/// <remarks><see href="https://docs.microsoft.com/zh-cn/windows/win32/api/sysinfoapi/nf-sysinfoapi-globalmemorystatusex"/></remarks>
|
||||
/// <param name="lpBuffer"></param>
|
||||
/// <returns></returns>
|
||||
[DllImport("Kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
|
||||
[return: MarshalAs(UnmanagedType.Bool)]
|
||||
public static extern Boolean GlobalMemoryStatusEx(ref MemoryStatusExE lpBuffer);
|
||||
#endif
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static MemoryValue GetMemory()
|
||||
{
|
||||
// 检查 Windows 内核版本,是否为旧系统
|
||||
if (Environment.OSVersion.Version.Major < 5)
|
||||
{
|
||||
// https://en.wikipedia.org/wiki/List_of_Microsoft_Windows_versions");
|
||||
return default;
|
||||
}
|
||||
|
||||
MemoryStatusExE memoryStatusEx = new MemoryStatusExE();
|
||||
// 初始化结构的大小
|
||||
memoryStatusEx.Init();
|
||||
// 刷新值
|
||||
if (!GlobalMemoryStatusEx(ref memoryStatusEx)) return default;
|
||||
|
||||
var TotalPhysicalMemory = memoryStatusEx.ullTotalPhys;
|
||||
var AvailablePhysicalMemory = memoryStatusEx.ullAvailPhys;
|
||||
var TotalVirtualMemory = memoryStatusEx.ullTotalVirtual;
|
||||
var AvailableVirtualMemory = memoryStatusEx.ullAvailVirtual;
|
||||
var UsedPercentage = memoryStatusEx.dwMemoryLoad;
|
||||
return new MemoryValue(
|
||||
TotalPhysicalMemory,
|
||||
AvailablePhysicalMemory,
|
||||
UsedPercentage,
|
||||
TotalVirtualMemory,
|
||||
AvailableVirtualMemory);
|
||||
}
|
||||
}
|
||||
}
|
||||
231
常用工具集/Utility/CZGL.SystemInfo/Network/NetworkInfo.cs
Normal file
231
常用工具集/Utility/CZGL.SystemInfo/Network/NetworkInfo.cs
Normal file
@@ -0,0 +1,231 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.NetworkInformation;
|
||||
using System.Net.Sockets;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace CZGL.SystemInfo
|
||||
{
|
||||
/// <summary>
|
||||
/// 网络接口信息
|
||||
/// </summary>
|
||||
public class NetworkInfo
|
||||
{
|
||||
private NetworkInterface _instance;
|
||||
|
||||
private NetworkInfo(NetworkInterface network)
|
||||
{
|
||||
_instance = network;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 当前实例使用的网络接口
|
||||
/// </summary>
|
||||
public NetworkInterface NetworkInterface => _instance;
|
||||
|
||||
|
||||
#region 基础信息
|
||||
|
||||
/// <summary>
|
||||
/// 获取网络适配器的标识符
|
||||
/// </summary>
|
||||
/// <remarks>ex:{92D3E528-5363-43C7-82E8-D143DC6617ED}</remarks>
|
||||
public string Id => _instance.Id;
|
||||
|
||||
/// <summary>
|
||||
/// 网络的 Mac 地址
|
||||
/// </summary>
|
||||
/// <remarks>ex: 1C997AF108E3</remarks>
|
||||
public string Mac => _instance.GetPhysicalAddress().ToString();
|
||||
|
||||
/// <summary>
|
||||
/// 网卡名称
|
||||
/// </summary>
|
||||
/// <remarks>ex:以太网,WLAN</remarks>
|
||||
public string Name => _instance.Name;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 描述网络接口的用户可读文本,
|
||||
/// 在 Windows 上,它通常描述接口供应商、类型 (例如,以太网) 、品牌和型号;
|
||||
/// </summary>
|
||||
/// <remarks>ex:Realtek PCIe GbE Family Controller、 Realtek 8822CE Wireless LAN 802.11ac PCI-E NIC</remarks>
|
||||
public string Trademark => _instance.Description;
|
||||
|
||||
/// <summary>
|
||||
/// 获取网络连接的当前操作状态<br />
|
||||
/// </summary>
|
||||
public OperationalStatus Status => _instance.OperationalStatus;
|
||||
|
||||
/// <summary>
|
||||
/// 获取网卡接口类型<br />
|
||||
/// </summary>
|
||||
public NetworkInterfaceType NetworkType => _instance.NetworkInterfaceType;
|
||||
|
||||
/// <summary>
|
||||
/// 网卡链接速度,每字节/秒为单位
|
||||
/// </summary>
|
||||
/// <remarks>如果是-1,则说明无法获取此网卡的链接速度;例如 270_000_000 表示是 270MB 的链接速度</remarks>
|
||||
public long Speed => _instance.Speed;
|
||||
|
||||
/// <summary>
|
||||
/// 是否支持 Ipv4
|
||||
/// </summary>
|
||||
public bool IsSupportIpv4 => _instance.Supports(NetworkInterfaceComponent.IPv4);
|
||||
|
||||
/// <summary>
|
||||
/// 获取分配给此接口的任意广播 IP 地址。只支持 Windows
|
||||
/// </summary>
|
||||
/// <remarks>一般情况下为空数组</remarks>
|
||||
public IPAddress[] AnycastAddresses
|
||||
{
|
||||
get
|
||||
{
|
||||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
||||
{
|
||||
return _instance.GetIPProperties().AnycastAddresses.Select(x => x.Address).ToArray();
|
||||
}
|
||||
else
|
||||
{
|
||||
return Array.Empty<IPAddress>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取分配给此接口的多播地址,ipv4、ipv6
|
||||
/// </summary>
|
||||
/// <remarks>ex:ff01::1%9 ff02::1%9<br />
|
||||
/// ff02::fb%9<br />
|
||||
/// ff02::1:3%9<br />
|
||||
/// ff02::1:ff61:9ae7%9<br />
|
||||
/// 224.0.0.1</remarks>
|
||||
public IPAddress[] MulticastAddresses => _instance.GetIPProperties().MulticastAddresses.Select(x => x.Address).ToArray();
|
||||
|
||||
/// <summary>
|
||||
/// 获取分配给此接口的单播地址,ipv4、ipv6
|
||||
/// </summary>
|
||||
/// <remarks>ex:192.168.3.38</remarks>
|
||||
public IPAddress[] UnicastAddresses => _instance.GetIPProperties().UnicastAddresses.Select(x => x.Address).ToArray();
|
||||
|
||||
/// <summary>
|
||||
/// 获取此接口的 IPv4 网关地址,ipv4、ipv6
|
||||
/// </summary>
|
||||
/// <remarks>ex:fe80::1677:40ff:fef9:bf95%5、192.168.3.1</remarks>
|
||||
public IPAddress[] GatewayAddresses => _instance.GetIPProperties().GatewayAddresses.Select(x => x.Address).ToArray();
|
||||
|
||||
/// <summary>
|
||||
/// 获取此接口的域名系统 (DNS) 服务器的地址,ipv4、ipv6
|
||||
/// </summary>
|
||||
/// <remarks>ex:fe80::1677:40ff:fef9:bf95%5、192.168.3.1</remarks>
|
||||
public IPAddress[] DnsAddresses => _instance.GetIPProperties().DnsAddresses.ToArray();
|
||||
|
||||
/// <summary>
|
||||
/// 是否支持 Ipv6
|
||||
/// </summary>
|
||||
public bool IsSupportIpv6 => _instance.Supports(NetworkInterfaceComponent.IPv6);
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 当前主机是否能够与其他计算机通讯(公网或内网),如果任何网络接口标记为 "up" 且不是环回或隧道接口,则认为网络连接可用。
|
||||
/// </summary>
|
||||
public static bool GetIsNetworkAvailable => NetworkInterface.GetIsNetworkAvailable();
|
||||
|
||||
/// <summary>
|
||||
/// 计算 IPV4 的网络流量
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="NotSupportedException">当前网卡不支持 IPV4</exception>
|
||||
public Rate GetIpv4Speed()
|
||||
{
|
||||
// 当前网卡不支持 IPV4
|
||||
if (!IsSupportIpv4) return default;
|
||||
var ipv4Statistics = _instance.GetIPv4Statistics();
|
||||
var speed = new Rate(DateTime.Now, ipv4Statistics.BytesReceived, ipv4Statistics.BytesSent);
|
||||
return speed;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 计算 IPV4 、IPV6 的网络流量
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public Rate IpvSpeed()
|
||||
{
|
||||
var ipvStatistics = _instance.GetIPStatistics();
|
||||
var speed = new Rate(DateTime.Now, ipvStatistics.BytesReceived, ipvStatistics.BytesSent);
|
||||
return speed;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取所有 IP 地址
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static IPAddress[] GetIPAddresses()
|
||||
{
|
||||
var hostName = Dns.GetHostName();
|
||||
return Dns.GetHostAddresses(hostName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取当前真实 IP
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static IPAddress TryGetRealIpv4()
|
||||
{
|
||||
var addrs = GetIPAddresses();
|
||||
var ipv4 = addrs.FirstOrDefault(x => x.AddressFamily == AddressFamily.InterNetwork);
|
||||
return ipv4;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取真实网卡
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static NetworkInfo TryGetRealNetworkInfo()
|
||||
{
|
||||
var realIp = TryGetRealIpv4();
|
||||
if (realIp == null)
|
||||
{
|
||||
return default;
|
||||
}
|
||||
var infos = NetworkInfo.GetNetworkInfos().ToArray();
|
||||
var info = infos.FirstOrDefault(x => x.UnicastAddresses.Any(i => i.MapToIPv4().ToString() == realIp.MapToIPv4().ToString()));
|
||||
if (info == null)
|
||||
{
|
||||
return default;
|
||||
}
|
||||
return info;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取此主机中所有网卡接口
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static NetworkInfo[] GetNetworkInfos()
|
||||
{
|
||||
return NetworkInterface.GetAllNetworkInterfaces().Select(x => new NetworkInfo(x)).ToArray();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 计算网络流量速率
|
||||
/// </summary>
|
||||
/// <param name="oldRate"></param>
|
||||
/// <param name="newRate"></param>
|
||||
/// <returns></returns>
|
||||
public static (SizeInfo Received, SizeInfo Sent) GetSpeed(Rate oldRate, Rate newRate)
|
||||
{
|
||||
var receive = newRate.ReceivedLength - oldRate.ReceivedLength;
|
||||
var send = newRate.SendLength - oldRate.SendLength;
|
||||
var interval = Math.Round((newRate.StartTime - oldRate.StartTime).TotalSeconds, 2);
|
||||
|
||||
long rSpeed = (long)(receive / interval);
|
||||
long sSpeed = (long)(send / interval);
|
||||
|
||||
return (SizeInfo.Get(rSpeed), SizeInfo.Get(sSpeed));
|
||||
}
|
||||
}
|
||||
}
|
||||
34
常用工具集/Utility/CZGL.SystemInfo/Network/Rate.cs
Normal file
34
常用工具集/Utility/CZGL.SystemInfo/Network/Rate.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
using System;
|
||||
|
||||
namespace CZGL.SystemInfo
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public struct Rate
|
||||
{
|
||||
public Rate(DateTime startTime, long receivedLength, long sendLength)
|
||||
{
|
||||
StartTime = startTime;
|
||||
ReceivedLength = receivedLength;
|
||||
SendLength = sendLength;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 记录时间
|
||||
/// </summary>
|
||||
public DateTime StartTime { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// 此网卡总接收网络流量字节数
|
||||
/// </summary>
|
||||
public long ReceivedLength { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// 此网卡总发送网络流量字节数
|
||||
/// </summary>
|
||||
public long SendLength { get; private set; }
|
||||
}
|
||||
|
||||
}
|
||||
61
常用工具集/Utility/CZGL.SystemInfo/SizeInfo.cs
Normal file
61
常用工具集/Utility/CZGL.SystemInfo/SizeInfo.cs
Normal file
@@ -0,0 +1,61 @@
|
||||
using System;
|
||||
|
||||
namespace CZGL.SystemInfo
|
||||
{
|
||||
/// <summary>
|
||||
/// 大小信息
|
||||
/// </summary>
|
||||
public struct SizeInfo
|
||||
{
|
||||
/// <summary>
|
||||
/// Byte 长度
|
||||
/// </summary>
|
||||
public long ByteLength { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// 大小
|
||||
/// </summary>
|
||||
public decimal Size { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 单位
|
||||
/// </summary>
|
||||
public UnitType SizeType { get; set; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 将字节单位转换为合适的单位
|
||||
/// </summary>
|
||||
/// <param name="byteLength">字节长度</param>
|
||||
/// <returns></returns>
|
||||
public static SizeInfo Get(long byteLength)
|
||||
{
|
||||
UnitType unit = 0;
|
||||
decimal number = byteLength;
|
||||
if (byteLength < 1000)
|
||||
{
|
||||
return new SizeInfo()
|
||||
{
|
||||
ByteLength = byteLength,
|
||||
Size = byteLength,
|
||||
SizeType = UnitType.B
|
||||
};
|
||||
}
|
||||
// 避免出现 1023B 这种情况;这样 1023B 会显示 0.99KB
|
||||
while (Math.Round(number / 1000) >= 1)
|
||||
{
|
||||
number = number / 1024;
|
||||
unit++;
|
||||
}
|
||||
|
||||
return new SizeInfo
|
||||
{
|
||||
Size = Math.Round(number, 2),
|
||||
SizeType = unit,
|
||||
ByteLength = byteLength
|
||||
};
|
||||
|
||||
throw new Exception();
|
||||
}
|
||||
}
|
||||
}
|
||||
133
常用工具集/Utility/CZGL.SystemInfo/System/SystemPlatformInfo.cs
Normal file
133
常用工具集/Utility/CZGL.SystemInfo/System/SystemPlatformInfo.cs
Normal file
@@ -0,0 +1,133 @@
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace CZGL.SystemInfo
|
||||
{
|
||||
/// <summary>
|
||||
/// 提供有关 .NET 运行时安装的信息、程序系统信息等。
|
||||
/// </summary>
|
||||
public static class SystemPlatformInfo
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// .NET Fx/Core Runtime version
|
||||
/// <para>ex: .NET Core 3.1.9</para>
|
||||
/// </summary>
|
||||
public static string FrameworkDescription => RuntimeInformation.FrameworkDescription;
|
||||
|
||||
/// <summary>
|
||||
/// .NET Fx/Core version
|
||||
/// <para>
|
||||
/// ex:<br />
|
||||
/// 3.1.9
|
||||
/// </para>
|
||||
/// </summary>
|
||||
public static string FrameworkVersion => Environment.Version.ToString();
|
||||
|
||||
/// <summary>
|
||||
/// 操作系统平台架构,可点击 <see cref="Architecture" /> 获取详细的信息
|
||||
/// <para>
|
||||
/// ex:<br />
|
||||
/// X86<br />
|
||||
/// X64<br />
|
||||
/// Arm<br />
|
||||
/// Arm64
|
||||
/// </para>
|
||||
/// </summary>
|
||||
public static string OSArchitecture => RuntimeInformation.OSArchitecture.ToString();
|
||||
|
||||
/// <summary>
|
||||
/// 获取操作系统的类型 <see cref="PlatformID"/>
|
||||
/// <para>
|
||||
/// ex:<br />
|
||||
/// Win32S、Win32Windows、Win32NT、WinCE、Unix、Xbox、MacOSX
|
||||
/// </para>
|
||||
/// </summary>
|
||||
public static string OSPlatformID => Environment.OSVersion.Platform.ToString();
|
||||
|
||||
/// <summary>
|
||||
/// 操作系统内核版本
|
||||
/// <para>
|
||||
/// ex:<br />
|
||||
/// Microsoft Windows NT 6.2.9200.0<br />
|
||||
/// Unix 4.4.0.19041
|
||||
/// </para>
|
||||
/// </summary>
|
||||
public static string OSVersion => Environment.OSVersion.ToString();
|
||||
|
||||
/// <summary>
|
||||
/// 操作系统的版本描述
|
||||
/// <para>
|
||||
/// ex: <br />
|
||||
/// Microsoft Windows 10.0.19041
|
||||
/// <br />
|
||||
/// Linux 4.4.0-19041-Microsoft #488-Microsoft Mon Sep 01 13:43:00 PST 2020
|
||||
/// </para>
|
||||
/// </summary>
|
||||
public static string OSDescription => RuntimeInformation.OSDescription;
|
||||
|
||||
/// <summary>
|
||||
/// 本进程的架构,可点击 <see cref="Architecture" /> 获取详细的信息
|
||||
/// <para>
|
||||
/// ex:<br />
|
||||
/// X86<br />
|
||||
/// X64<br />
|
||||
/// Arm<br />
|
||||
/// Arm64
|
||||
/// </para>
|
||||
/// </summary>
|
||||
public static string ProcessArchitecture => RuntimeInformation.ProcessArchitecture.ToString();
|
||||
|
||||
/// <summary>
|
||||
/// 当前计算机上的处理器数
|
||||
/// </summary>
|
||||
/// <remarks>如 4核心8线程的 CPU,这里会获取到 8</remarks>
|
||||
public static int ProcessorCount => Environment.ProcessorCount;
|
||||
|
||||
/// <summary>
|
||||
/// 计算机名称
|
||||
/// </summary>
|
||||
public static string MachineName => Environment.MachineName;
|
||||
|
||||
/// <summary>
|
||||
/// 当前登录到此系统的用户名称
|
||||
/// </summary>
|
||||
public static string UserName => Environment.UserName;
|
||||
|
||||
/// <summary>
|
||||
/// 用户网络域名称,即 hostname
|
||||
/// </summary>
|
||||
public static string UserDomainName => Environment.UserDomainName;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 是否在交互模式中运行
|
||||
/// </summary>
|
||||
public static bool IsUserInteractive => Environment.UserInteractive;
|
||||
|
||||
/// <summary>
|
||||
/// 系统的磁盘和分区列表
|
||||
/// <para>
|
||||
/// ex:<br />
|
||||
/// Windows: D:\, E:\, F:\, G:\, H:\, J:\, X:\<br />
|
||||
/// Linux: /, /dev, /sys, /proc, /dev/pts, /run, /run/lock, /run/shm ...
|
||||
/// </para>
|
||||
/// </summary>
|
||||
public static string[] GetLogicalDrives => Environment.GetLogicalDrives();
|
||||
|
||||
/// <summary>
|
||||
/// 系统根目录完全路径。<b>Linux 没有系统根目录</b>
|
||||
/// <para>
|
||||
/// ex:<br />
|
||||
/// Windows: X:\WINDOWS\system32<br></br>
|
||||
/// Linux : null
|
||||
/// </para>
|
||||
/// </summary>
|
||||
public static string SystemDirectory => Environment.SystemDirectory;
|
||||
|
||||
/// <summary>
|
||||
/// 操作系统内存页一页的字节数
|
||||
/// </summary>
|
||||
public static int MemoryPageSize => Environment.SystemPageSize;
|
||||
}
|
||||
}
|
||||
38
常用工具集/Utility/CZGL.SystemInfo/UnitType.cs
Normal file
38
常用工具集/Utility/CZGL.SystemInfo/UnitType.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
namespace CZGL.SystemInfo
|
||||
{
|
||||
/// <summary>
|
||||
/// 单位
|
||||
/// </summary>
|
||||
public enum UnitType : int
|
||||
{
|
||||
/// <summary>
|
||||
/// Byte
|
||||
/// </summary>
|
||||
///
|
||||
B = 0,
|
||||
/// <summary>
|
||||
/// KB
|
||||
/// </summary>
|
||||
KB,
|
||||
|
||||
/// <summary>
|
||||
/// MB
|
||||
/// </summary>
|
||||
MB,
|
||||
|
||||
/// <summary>
|
||||
/// GB
|
||||
/// </summary>
|
||||
GB,
|
||||
|
||||
/// <summary>
|
||||
/// TB
|
||||
/// </summary>
|
||||
TB,
|
||||
|
||||
/// <summary>
|
||||
/// PB
|
||||
/// </summary>
|
||||
PB
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user