ApplicationHelper
ApplicationHelper 帮助类
Section titled “ApplicationHelper 帮助类”ApplicationHelper 是一个静态帮助类,为应用程序提供了丰富的信息和控制功能,包括应用程序信息获取、系统环境信息检测、硬件信息获取、运行时监控、应用程序控制、环境变量管理等,旨在简化应用程序信息获取和系统环境检测的开发工作。
- 📱 应用程序信息获取(名称、版本、路径等)
- 🖥️ 系统环境信息检测(操作系统、架构、区域设置等)
- 💾 硬件信息获取(处理器、磁盘、网络接口等)
- 📊 运行时监控(进程信息、GC 信息、性能监控等)
- 🔧 应用程序控制(重启、退出、权限检测等)
- 🌍 环境变量管理
public static partial class ApplicationHelper应用程序基本信息
Section titled “应用程序基本信息”GetApplicationName
Section titled “GetApplicationName”获取当前应用程序的名称。
public static string GetApplicationName()返回值: 应用程序名称,如果无法获取则返回 “Unknown”。
GetApplicationFullName
Section titled “GetApplicationFullName”获取当前应用程序的完整名称(包含版本信息)。
public static string GetApplicationFullName()返回值: 应用程序完整名称,格式为 “名称 版本号”。
GetApplicationVersion
Section titled “GetApplicationVersion”获取当前应用程序的版本。
public static Version GetApplicationVersion()返回值: 应用程序版本,如果无法获取则返回 0.0.0.0。
GetApplicationVersionString
Section titled “GetApplicationVersionString”获取当前应用程序的版本字符串。
public static string GetApplicationVersionString(int fieldCount = 3)| 参数名 | 类型 | 描述 | |--------|------|------| | fieldCount | int | 版本字段数量(1-4),默认为 3 |
GetApplicationPath
Section titled “GetApplicationPath”获取当前应用程序的启动路径(基目录)。
public static string GetApplicationPath()GetExecutablePath
Section titled “GetExecutablePath”获取当前应用程序的可执行文件路径。
public static string GetExecutablePath()注意: 在单文件发布模式下,此方法可能返回空字符串。
GetConfigurationPath
Section titled “GetConfigurationPath”获取当前应用程序的配置文件路径。
public static string GetConfigurationPath()GetAppDomainInfo
Section titled “GetAppDomainInfo”获取应用程序域信息。
public static AppDomainInfo GetAppDomainInfo()返回值: AppDomainInfo 对象,包含 FriendlyName、BaseDirectory、RelativeSearchPath、ShadowCopyFiles、IsFullyTrusted 属性。
GetProcessId
Section titled “GetProcessId”获取当前进程 ID。
public static int GetProcessId()GetProcessName
Section titled “GetProcessName”获取当前进程名称。
public static string GetProcessName()GetStartTime
Section titled “GetStartTime”获取应用程序启动时间。
public static DateTime GetStartTime()GetUptime
Section titled “GetUptime”获取应用程序运行时长。
public static TimeSpan GetUptime()系统环境信息
Section titled “系统环境信息”GetOperatingSystemInfo
Section titled “GetOperatingSystemInfo”获取操作系统信息。
public static OperatingSystemInfo GetOperatingSystemInfo()返回值: OperatingSystemInfo 对象,包含 Platform、Version、ServicePack、Is64Bit、MachineName、UserName、UserDomainName 属性。
GetOSDescription
Section titled “GetOSDescription”获取操作系统描述字符串。
public static string GetOSDescription()GetSystemArchitecture
Section titled “GetSystemArchitecture”获取当前系统架构。
public static string GetSystemArchitecture()返回值: 系统架构(x64 或 x86)。
GetRuntimeArchitecture
Section titled “GetRuntimeArchitecture”获取当前运行时架构。
public static string GetRuntimeArchitecture()GetFrameworkDescription
Section titled “GetFrameworkDescription”获取运行时框架描述。
public static string GetFrameworkDescription()IsWindows
Section titled “IsWindows”检查操作系统是否为 Windows。
public static bool IsWindows()IsLinux
Section titled “IsLinux”检查操作系统是否为 Linux。
public static bool IsLinux()IsMacOS
Section titled “IsMacOS”检查操作系统是否为 macOS。
public static bool IsMacOS()GetCurrentPlatform
Section titled “GetCurrentPlatform”获取当前操作系统平台。
public static string GetCurrentPlatform()返回值: 操作系统平台名称(Windows、Linux、macOS 或 Unknown)。
GetSystemCulture
Section titled “GetSystemCulture”获取系统区域信息。
public static CultureInfo GetSystemCulture()GetSystemUICulture
Section titled “GetSystemUICulture”获取系统 UI 区域信息。
public static CultureInfo GetSystemUICulture()GetSystemTimeZone
Section titled “GetSystemTimeZone”获取系统时区信息。
public static TimeZoneInfo GetSystemTimeZone()GetSystemTimeZoneName
Section titled “GetSystemTimeZoneName”获取系统时区名称。
public static string GetSystemTimeZoneName()GetMachineName
Section titled “GetMachineName”获取计算机名称。
public static string GetMachineName()GetUserName
Section titled “GetUserName”获取当前用户名。
public static string GetUserName()GetUserDomainName
Section titled “GetUserDomainName”获取用户域名。
public static string GetUserDomainName()GetSystemDirectory
Section titled “GetSystemDirectory”获取系统目录路径。
public static string GetSystemDirectory()GetProcessorCount
Section titled “GetProcessorCount”获取处理器核心数。
public static int GetProcessorCount()GetTotalMemory
Section titled “GetTotalMemory”获取系统总内存(字节)。
public static long GetTotalMemory()GetAvailableMemory
Section titled “GetAvailableMemory”获取可用内存(字节)。
public static long GetAvailableMemory()GetDiskDriveInfo
Section titled “GetDiskDriveInfo”获取磁盘驱动器信息列表。
public static List<DiskDriveInfo> GetDiskDriveInfo()GetNetworkInterfaces
Section titled “GetNetworkInterfaces”获取网络接口信息列表。
public static List<NetworkInterfaceInfo> GetNetworkInterfaces()GetWorkingSet
Section titled “GetWorkingSet”获取工作集内存大小(字节)。
public static long GetWorkingSet()GetPeakWorkingSet
Section titled “GetPeakWorkingSet”获取峰值工作集内存大小(字节)。
public static long GetPeakWorkingSet()GetVirtualMemorySize
Section titled “GetVirtualMemorySize”获取虚拟内存大小(字节)。
public static long GetVirtualMemorySize()GetThreadCount
Section titled “GetThreadCount”获取线程数。
public static int GetThreadCount()GetHandleCount
Section titled “GetHandleCount”获取句柄数。
public static int GetHandleCount()GetGCTotalMemory
Section titled “GetGCTotalMemory”获取 GC 总内存(字节)。
public static long GetGCTotalMemory()GetGCGenerationInfo
Section titled “GetGCGenerationInfo”获取 GC 代信息。
public static GCGenerationInfo GetGCGenerationInfo()ForceGC
Section titled “ForceGC”强制执行垃圾回收。
public static void ForceGC(int generation = 2)应用程序控制
Section titled “应用程序控制”IsAdministrator
Section titled “IsAdministrator”检查当前进程是否以管理员权限运行。
public static bool IsAdministrator()Restart
Section titled “Restart”重启应用程序。
public static void Restart(int exitCode = 0)退出应用程序。
public static void Exit(int exitCode = 0)EnsureSingleInstance
Section titled “EnsureSingleInstance”确保应用程序单实例运行。
public static bool EnsureSingleInstance(string mutexName)返回值: 如果是第一个实例返回 true,否则返回 false。
环境变量管理
Section titled “环境变量管理”GetEnvironmentVariable
Section titled “GetEnvironmentVariable”获取环境变量值。
public static string GetEnvironmentVariable(string variableName)SetEnvironmentVariable
Section titled “SetEnvironmentVariable”设置环境变量值。
public static void SetEnvironmentVariable(string variableName, string value)GetEnvironmentVariables
Section titled “GetEnvironmentVariables”获取所有环境变量。
public static Dictionary<string, string> GetEnvironmentVariables()获取应用程序基本信息
Section titled “获取应用程序基本信息”using Chet.Utils.Helpers;
// 获取应用程序名称var appName = ApplicationHelper.GetApplicationName();Console.WriteLine($"应用程序名称: {appName}");
// 获取应用程序版本var version = ApplicationHelper.GetApplicationVersion();Console.WriteLine($"应用程序版本: {version}");
// 获取应用程序路径var path = ApplicationHelper.GetApplicationPath();Console.WriteLine($"应用程序路径: {path}");
// 获取运行时长var uptime = ApplicationHelper.GetUptime();Console.WriteLine($"已运行: {uptime.TotalHours:F2} 小时");获取系统环境信息
Section titled “获取系统环境信息”// 获取操作系统信息var osInfo = ApplicationHelper.GetOperatingSystemInfo();Console.WriteLine($"操作系统: {osInfo.Platform}");Console.WriteLine($"版本: {osInfo.Version}");Console.WriteLine($"64位: {osInfo.Is64Bit}");
// 检测操作系统平台if (ApplicationHelper.IsWindows()){ Console.WriteLine("当前运行在 Windows 系统");}else if (ApplicationHelper.IsLinux()){ Console.WriteLine("当前运行在 Linux 系统");}
// 获取时区信息var timeZone = ApplicationHelper.GetSystemTimeZone();Console.WriteLine($"时区: {timeZone.DisplayName}");获取硬件信息
Section titled “获取硬件信息”// 获取处理器核心数var processorCount = ApplicationHelper.GetProcessorCount();Console.WriteLine($"处理器核心数: {processorCount}");
// 获取内存信息var totalMemory = ApplicationHelper.GetTotalMemory();var availableMemory = ApplicationHelper.GetAvailableMemory();Console.WriteLine($"总内存: {totalMemory / 1024 / 1024} MB");Console.WriteLine($"可用内存: {availableMemory / 1024 / 1024} MB");
// 获取磁盘信息var disks = ApplicationHelper.GetDiskDriveInfo();foreach (var disk in disks){ Console.WriteLine($"磁盘 {disk.Name}: {disk.TotalSize / 1024 / 1024 / 1024} GB");}// 获取进程内存使用var workingSet = ApplicationHelper.GetWorkingSet();Console.WriteLine($"工作集内存: {workingSet / 1024 / 1024} MB");
// 获取线程数var threadCount = ApplicationHelper.GetThreadCount();Console.WriteLine($"线程数: {threadCount}");
// 获取 GC 内存var gcMemory = ApplicationHelper.GetGCTotalMemory();Console.WriteLine($"GC 总内存: {gcMemory / 1024 / 1024} MB");权限检测和单实例
Section titled “权限检测和单实例”// 检查是否以管理员权限运行if (ApplicationHelper.IsAdministrator()){ Console.WriteLine("当前以管理员权限运行");}else{ Console.WriteLine("当前以普通用户权限运行");}
// 确保单实例运行if (!ApplicationHelper.EnsureSingleInstance("MyUniqueAppName")){ Console.WriteLine("应用程序已在运行,退出..."); ApplicationHelper.Exit(1);}AppDomainInfo
Section titled “AppDomainInfo”| 属性名 | 类型 | 描述 | |--------|------|------| | FriendlyName | string | 应用程序域友好名称 | | BaseDirectory | string | 基目录 | | RelativeSearchPath | string | 相对搜索路径 | | ShadowCopyFiles | bool | 是否启用卷影复制 | | IsFullyTrusted | bool | 是否完全受信任 |
OperatingSystemInfo
Section titled “OperatingSystemInfo”| 属性名 | 类型 | 描述 | |--------|------|------| | Platform | string | 操作系统平台 | | Version | string | 版本号 | | ServicePack | string | 服务包 | | Is64Bit | bool | 是否为 64 位 | | MachineName | string | 计算机名 | | UserName | string | 用户名 | | UserDomainName | string | 用户域名 |
DiskDriveInfo
Section titled “DiskDriveInfo”| 属性名 | 类型 | 描述 | |--------|------|------| | Name | string | 驱动器名称 | | TotalSize | long | 总大小(字节) | | AvailableFreeSpace | long | 可用空间(字节) | | DriveFormat | string | 文件系统格式 | | DriveType | string | 驱动器类型 |
NetworkInterfaceInfo
Section titled “NetworkInterfaceInfo”| 属性名 | 类型 | 描述 | |--------|------|------| | Name | string | 接口名称 | | Description | string | 描述 | | Status | string | 状态 | | Speed | long | 速度(bps) | | MacAddress | string | MAC 地址 |
- 部分方法在不同操作系统上可能返回不同的结果。
- 获取硬件信息可能需要相应的权限。
- 单实例检测使用 Mutex 实现,确保 mutexName 全局唯一。
- 在单文件发布模式下,GetExecutablePath 可能返回空字符串。
- .NET Framework 4.6.1 及以上版本
- .NET Core 2.0 及以上版本
- .NET 5.0 及以上版本
- .NET Standard 2.0 及以上版本