标签: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<OSItem> items) { this.Version = version; this.Items = items; } public Version Version { get; private set; } public List<OSItem> 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<OSItem>(); 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<OSItem>(); 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"> <!-- UAC 清单选项 如果要更改 Windows 用户帐户控制级别,请用以下节点之一替换 requestedExecutionLevel 节点。 <requestedExecutionLevel level="asInvoker" uiAccess="false" /> <requestedExecutionLevel level="requireAdministrator" uiAccess="false" /> <requestedExecutionLevel level="highestAvailable" uiAccess="false" /> 指定 requestedExecutionLevel 节点将会禁用文件和注册表虚拟化。 如果要利用文件和注册表虚拟化实现向后 兼容性,则删除 requestedExecutionLevel 节点。 --> <requestedExecutionLevel level="asInvoker" uiAccess="false" /> </requestedPrivileges> </security> </trustInfo> <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1"> <application> <!-- 此应用程序设计使用的所有 Windows 版本的列表。Windows 将会自动选择最兼容的环境。--> <!-- 如果应用程序设计使用 Windows 7,请取消注释以下 supportedOS 节点--> <supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" /> </application> </compatibility> <!-- 启用 Windows 公共控件和对话框的主题(Windows XP 和更高版本) --> <!-- <dependency> <dependentAssembly> <assemblyIdentity type="win32" name="Microsoft.Windows.Common-Controls" version="6.0.0.0" processorArchitecture="*" publicKeyToken="6595b64144ccf1df" language="*" /> </dependentAssembly> </dependency>--> </asmv1:assembly>
运行结果:
Version:10.0
Name:Windows Server 2019|Windows Server 2016
标签:mon 环境 dmi static comm require dex 微软官方 iss
原文地址:https://www.cnblogs.com/nanfei/p/14663875.html