C# 获取操作系统信息

2021-05-15 20:30

阅读:539

标签:mon   环境   dmi   static   comm   require   dex   微软官方   iss   

参见微软官方 https://docs.microsoft.com/en-us/windows/win32/sysinfo/operating-system-version
using
System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.InteropServices; namespace ConsoleApplication4 { class Program { static void Main(string[] args) { var osInfo = OSHelper.getCurrentOSInfo(); Console.WriteLine("Version:" + osInfo.Version.ToString()); Console.WriteLine("Name:" + string.Join("|", osInfo.Items.Select(x => x.Name))); Console.ReadLine(); } } public static class OSHelper { public class OSInfo { public OSInfo(Version version, List items) { this.Version = version; this.Items = items; } public Version Version { get; private set; } public List Items { get; private set; } } public class OSItem { public OSItem(string name, Version version, bool isServer) { this.Name = name; this.Version = version; this.IsServer = isServer; } public string Name { get; private set; } public Version Version { get; private set; } public bool IsServer { get; private set; } } [DllImport("shlwapi.dll", SetLastError = true, EntryPoint = "#437")] private static extern bool IsOS(int os); const int OS_ANYSERVER = 29; private static bool IsWindowsServer() { return IsOS(OS_ANYSERVER); } public static OSInfo GetOSInfoFromVersion(Version ver) { var osList = new List(); osList.Add(new OSItem("Windows 10", new Version("10.0"), false)); osList.Add(new OSItem("Windows Server 2019", new Version("10.0"), true)); osList.Add(new OSItem("Windows Server 2016", new Version("10.0"), true)); osList.Add(new OSItem("Windows 8.1", new Version("6.3"), false)); osList.Add(new OSItem("Windows Server 2012 R2", new Version("6.3"), true)); osList.Add(new OSItem("Windows 8", new Version("6.2"), false)); osList.Add(new OSItem("Windows Server 2012", new Version("6.2"), true)); osList.Add(new OSItem("Windows 7", new Version("6.1 "), false)); osList.Add(new OSItem("Windows Server 2008 R2", new Version("6.1"), true)); osList.Add(new OSItem("Windows Server 2008", new Version("6.0"), true)); osList.Add(new OSItem("Windows Vista", new Version("6.0"), false)); osList.Add(new OSItem("Windows Server 2003 R2", new Version("5.2"), true)); osList.Add(new OSItem("Windows Server 2003", new Version("5.2"), true)); osList.Add(new OSItem("Windows XP 64-Bit Edition", new Version("5.2"), false)); osList.Add(new OSItem("Windows XP", new Version("5.1"), false)); osList.Add(new OSItem("Windows 2000", new Version("5.0"), false)); var os = osList.GroupBy(x => x.Version) .ToDictionary(k => k.Key, v => v.ToList()); var minVersion = os.FirstOrDefault(x => x.Key == os.Min(y => y.Key)); var maxVersion = os.FirstOrDefault(x => x.Key == os.Max(y => y.Key)); if (ver minVersion.Key) return new OSInfo(minVersion.Key, minVersion.Value.ToList()); if (ver > os.Max(x => x.Key)) return new OSInfo(maxVersion.Key, maxVersion.Value.ToList()); var item = os.OrderByDescending(x => x.Key).SkipWhile(x => x.Key > ver).FirstOrDefault(); return new OSInfo(item.Key, item.Value.ToList()); } public static OSInfo getCurrentOSInfo() { var isServer = IsWindowsServer(); var osInfo = GetOSInfoFromVersion(Environment.OSVersion.Version); var osItems = new List(); foreach (var osItem in osInfo.Items) { if (osItem.IsServer == isServer) osItems.Add(osItem); } return new OSInfo(osInfo.Version, osItems); } } }

清单文件:

xml version="1.0" encoding="utf-8"?>
asmv1:assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
  trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
    security>
      requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
        
        requestedExecutionLevel level="asInvoker" uiAccess="false" />
      requestedPrivileges>
    security>
  trustInfo>
  
  compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
    application>
      

      
      supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />
      
    application>
  compatibility>
  
  
  

asmv1:assembly>

运行结果:

Version:10.0
Name:Windows Server 2019|Windows Server 2016

C# 获取操作系统信息

标签:mon   环境   dmi   static   comm   require   dex   微软官方   iss   

原文地址:https://www.cnblogs.com/nanfei/p/14663875.html


评论


亲,登录后才可以留言!