C#获取Windows相关信息


发表于 修改于 后端随手写写 229 字 1 分钟
using Microsoft.Win32;
using System;
using System.Management;
using System.Runtime.InteropServices;
using SLID = System.Guid;
namespace Coderxi.Util {
public class WindowsInfo {
public enum SL_GENUINE_STATE {
SL_GEN_STATE_IS_GENUINE = 0,
SL_GEN_STATE_INVALID_LICENSE = 1,
SL_GEN_STATE_TAMPERED = 2,
SL_GEN_STATE_LAST = 3
}
[DllImportAttribute("Slwga.dll", EntryPoint = "SLIsGenuineLocal", CharSet = CharSet.None, ExactSpelling = false, SetLastError = false, PreserveSig = true, CallingConvention = CallingConvention.Winapi, BestFitMapping = false, ThrowOnUnmappableChar = false)]
[PreserveSigAttribute()]
internal static extern uint SLIsGenuineLocal(ref SLID slid, [In, Out] ref SL_GENUINE_STATE genuineState, IntPtr val3);
/// <summary>
/// Windows系统构建版本
/// </summary>
public static string WindowsReleaseId => Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion", "ReleaseId", "").ToString();
/// <summary>
/// Windows是否激活
/// </summary>
private static bool WindowsIsActive {
get {
bool _IsGenuineWindows = false;
Guid ApplicationID = new Guid("55c92734-d682-4d71-983e-d6ec3f16059f"); //Application ID GUID http://technet.microsoft.com/en-us/library/dd772270.aspx
SLID windowsSlid = (Guid)ApplicationID;
SL_GENUINE_STATE genuineState = SL_GENUINE_STATE.SL_GEN_STATE_LAST;
uint result = SLIsGenuineLocal(ref windowsSlid, ref genuineState, IntPtr.Zero);
if (result == 0) _IsGenuineWindows = (genuineState == SL_GENUINE_STATE.SL_GEN_STATE_IS_GENUINE);
return _IsGenuineWindows;
}
}
/// <summary>
/// Windows激活状态
/// </summary>
public static string WindowsActiveState => Environment.OSVersion.Version.Major < 6 ? "不支持的系统" : WindowsIsActive ? "已激活" : "未激活";
/// <summary>
/// Windows系统版本
/// </summary>
public static string WindowsVersion {
get {
using (ManagementObjectSearcher win32OperatingSystem = new ManagementObjectSearcher("select * from Win32_OperatingSystem")) {
foreach (ManagementObject obj in win32OperatingSystem.Get())
return $"{obj["Caption"]} ({obj["OSArchitecture"]})";
}
return "未知版本";
}
}
}
}

评论