Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何检测我的应用程序是否在Windows 10上运行_C#_Windows 10 - Fatal编程技术网

C# 如何检测我的应用程序是否在Windows 10上运行

C# 如何检测我的应用程序是否在Windows 10上运行,c#,windows-10,C#,Windows 10,我正在寻找一种方法来检测我的C#应用程序是否在Windows 10上运行 我曾希望Environment.OSVersion能起到作用,但这似乎在Windows8.1和Windows10上返回了6.3.9600.0的Version 其他解决方案,如Windows 8和Windows 10,似乎也没有区别 有什么建议吗 我为什么要这样做 因为我使用WinForms WebBrowser控件来托管OAuth页面,该页面在旧版IE中崩溃并烧坏(我的应用程序连接到了…) 默认情况下,WebBrowse

我正在寻找一种方法来检测我的C#应用程序是否在Windows 10上运行

我曾希望
Environment.OSVersion
能起到作用,但这似乎在Windows8.1和Windows10上返回了
6.3.9600.0
Version

其他解决方案,如Windows 8和Windows 10,似乎也没有区别

有什么建议吗


我为什么要这样做

因为我使用WinForms WebBrowser控件来托管OAuth页面,该页面在旧版IE中崩溃并烧坏(我的应用程序连接到了…)


默认情况下,WebBrowser控件模拟IE7。使用注册表项,您可以告诉它模拟安装在主机PC上的最新版本的IE。但是,Windows 8.1(以及Windows 10的预发行版)在最终版本的Windows 10中不起作用。

您是否尝试过以下操作?[您需要添加对
Microsoft.visualbasic
dll]的引用]

new Microsoft.VisualBasic.Devices.ComputerInfo().OSFullName

在我的计算机上,它提供Microsoft Windows 7 Ultimate

如果查看注册表,您将发现环境名称:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProductName
例如,我的产品名是
windows10home

如果使用Windows 10,您将获得以下代码:

 static bool IsWindows10()
 {
     var reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion");

     string productName = (string)reg.GetValue("ProductName");

     return productName.StartsWith("Windows 10");
 }
注意:使用Microsoft.Win32添加
用于您的使用。

在引擎盖下,使用已弃用的。文档警告您观察到的行为:

未针对Windows 8.1或Windows 10显示的应用程序将返回Windows 8 OS版本值(6.2)

文件还建议:

识别当前操作系统通常不是确定是否存在特定操作系统功能的最佳方法。这是因为操作系统可能在可再发行的DLL中添加了新功能。不要使用GetVersionEx来确定操作系统平台或版本号,而是测试功能本身是否存在

如果上述建议不适合您的情况,并且您确实希望检查实际运行的操作系统版本,那么文档还提供了一个提示:

要将当前系统版本与所需版本进行比较,请使用VerifyVersionInfo功能,而不是使用GetVersionEx自行执行比较

以下文章发布了一个使用VerifyVersionInfo函数的有效解决方案:

下面的代码片段应该提供您正在寻找的行为,这完全归功于该文章的作者:

public class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine(IsWindowsVersionOrGreater(6, 3, 0)); // Plug in appropriate values.
    }

    [StructLayout(LayoutKind.Sequential)]
    struct OsVersionInfoEx
    {
        public uint OSVersionInfoSize;
        public uint MajorVersion;
        public uint MinorVersion;
        public uint BuildNumber;
        public uint PlatformId;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
        public string CSDVersion;
        public ushort ServicePackMajor;
        public ushort ServicePackMinor;
        public ushort SuiteMask;
        public byte ProductType;
        public byte Reserved;
    }

    [DllImport("kernel32.dll")]
    static extern ulong VerSetConditionMask(ulong dwlConditionMask,
       uint dwTypeBitMask, byte dwConditionMask);
    [DllImport("kernel32.dll")]
    static extern bool VerifyVersionInfo(
        [In] ref OsVersionInfoEx lpVersionInfo,
        uint dwTypeMask, ulong dwlConditionMask);

    static bool IsWindowsVersionOrGreater(
        uint majorVersion, uint minorVersion, ushort servicePackMajor)
    {
        OsVersionInfoEx osvi = new OsVersionInfoEx();
        osvi.OSVersionInfoSize = (uint)Marshal.SizeOf(osvi);
        osvi.MajorVersion = majorVersion;
        osvi.MinorVersion = minorVersion;
        osvi.ServicePackMajor = servicePackMajor;
        // These constants initialized with corresponding definitions in
        // winnt.h (part of Windows SDK)
        const uint VER_MINORVERSION = 0x0000001;
        const uint VER_MAJORVERSION = 0x0000002;
        const uint VER_SERVICEPACKMAJOR = 0x0000020;
        const byte VER_GREATER_EQUAL = 3;
        ulong versionOrGreaterMask = VerSetConditionMask(
            VerSetConditionMask(
                VerSetConditionMask(
                    0, VER_MAJORVERSION, VER_GREATER_EQUAL),
                VER_MINORVERSION, VER_GREATER_EQUAL),
            VER_SERVICEPACKMAJOR, VER_GREATER_EQUAL);
        uint versionOrGreaterTypeMask = VER_MAJORVERSION |
            VER_MINORVERSION | VER_SERVICEPACKMAJOR;
        return VerifyVersionInfo(ref osvi, versionOrGreaterTypeMask,
            versionOrGreaterMask);
    }
}

免责声明:我还没有Windows 10,所以我还没有在Windows 10上测试代码。

答案

使用
Environment.OSVersion
并添加一个应用程序清单文件,其中包含未注释的相关
supportedOS
元素

e、 g.在以下内容中添加:

Windows 10:VerifyVersionInfo在被调用时返回false 没有Windows 8.1兼容清单的应用程序 如果lpVersionInfo参数设置为 指定Windows 8.1或Windows 10,即使当前操作系统 系统版本为Windows 8.1或Windows 10。明确地 VerifyVersionInfo具有以下行为:

•如果应用程序没有清单,VerifyVersionInfo的行为就像操作系统版本是Windows 8(6.2)一样

•如果应用程序的清单包含与Windows 8.1相对应的GUID,VerifyVersionInfo的行为就好像操作系统版本是Windows 8.1(6.3)

•如果应用程序具有包含对应GUID的清单 对于Windows 10,VerifyVersionInfo的行为就像操作系统 版本为Windows 10(10.0)

原因是Windows 10中不推荐使用VerifyVersionInfo


我已经在Windows 10和
环境上进行了测试。当app.Manifest包含如上所述的相关GUID时,OSVersion
完全按照预期工作。这很可能就是他们没有从.Net Framework中更改或弃用它的原因。

我建议使用注册表来查找所需的值。微软已经改变了Windows10在注册表中列出的方式,因此需要对代码进行调整

下面是我使用的代码,它也正确地标识了Windows 10:

namespace Inspection
{
    /// <summary>
    /// Static class that adds convenient methods for getting information on the running computers basic hardware and os setup.
    /// </summary>
    public static class ComputerInfo
    {
        /// <summary>
        ///     Returns the Windows major version number for this computer.
        /// </summary>
        public static uint WinMajorVersion
        {
            get
            {
                dynamic major;
                // The 'CurrentMajorVersionNumber' string value in the CurrentVersion key is new for Windows 10, 
                // and will most likely (hopefully) be there for some time before MS decides to change this - again...
                if (TryGetRegistryKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "CurrentMajorVersionNumber", out major))
                {
                    return (uint) major;
                }

                // When the 'CurrentMajorVersionNumber' value is not present we fallback to reading the previous key used for this: 'CurrentVersion'
                dynamic version;
                if (!TryGetRegistryKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "CurrentVersion", out version))
                    return 0;

                var versionParts = ((string) version).Split('.');
                if (versionParts.Length != 2) return 0;
                uint majorAsUInt;
                return uint.TryParse(versionParts[0], out majorAsUInt) ? majorAsUInt : 0;
            }
        }

        /// <summary>
        ///     Returns the Windows minor version number for this computer.
        /// </summary>
        public static uint WinMinorVersion
        {
            get
            {
                dynamic minor;
                // The 'CurrentMinorVersionNumber' string value in the CurrentVersion key is new for Windows 10, 
                // and will most likely (hopefully) be there for some time before MS decides to change this - again...
                if (TryGetRegistryKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "CurrentMinorVersionNumber",
                    out minor))
                {
                    return (uint) minor;
                }

                // When the 'CurrentMinorVersionNumber' value is not present we fallback to reading the previous key used for this: 'CurrentVersion'
                dynamic version;
                if (!TryGetRegistryKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "CurrentVersion", out version))
                    return 0;

                var versionParts = ((string) version).Split('.');
                if (versionParts.Length != 2) return 0;
                uint minorAsUInt;
                return uint.TryParse(versionParts[1], out minorAsUInt) ? minorAsUInt : 0;
            }
        }

        /// <summary>
        ///     Returns whether or not the current computer is a server or not.
        /// </summary>
        public static uint IsServer
        {
            get
            {
                dynamic installationType;
                if (TryGetRegistryKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "InstallationType",
                    out installationType))
                {
                    return (uint) (installationType.Equals("Client") ? 0 : 1);
                }

                return 0;
            }
        }

        private static bool TryGetRegistryKey(string path, string key, out dynamic value)
        {
            value = null;
            try
            {
                var rk = Registry.LocalMachine.OpenSubKey(path);
                if (rk == null) return false;
                value = rk.GetValue(key);
                return value != null;
            }
            catch
            {
                return false;
            }
        }
    }
}
名称空间检查
{
/// 
///静态类,该类添加了方便的方法,用于获取有关正在运行的计算机的基本硬件和操作系统设置的信息。
/// 
公共静态类计算机信息
{
/// 
///返回此计算机的Windows主版本号。
/// 
公共静态uint WinMajorVersion
{
得到
{
动态专业;
//CurrentVersion密钥中的“CurrentMajorVersionNumber”字符串值对于Windows 10是新的,
//而且很可能(希望)在MS决定改变这一点之前会有一段时间——再次。。。
if(TryGetRegistryKey(@“SOFTWARE\Microsoft\Windows NT\CurrentVersion”,“CurrentMajorVersionNumber”,out major))
{
返回(uint)主要;
}
//当“CurrentMajorVersionNumber”值不存在时,我们回退到读取用于此的上一个键:“CurrentVersion”
动态版本;
如果(!TryGetRegistryKey(@“SOFTWARE\Microsoft\Windows NT\CurrentVersion”,“CurrentVersion”,out version))
返回0;
var versionParts=((字符串)版本).Split('.');
如果(versionParts.Length!=2)返回0;
主要目的地;
返回uint.TryParse(versionParts[0],out majorAsUInt)?majorAsUInt:0;
}
}
/// 
///
namespace Inspection
{
    /// <summary>
    /// Static class that adds convenient methods for getting information on the running computers basic hardware and os setup.
    /// </summary>
    public static class ComputerInfo
    {
        /// <summary>
        ///     Returns the Windows major version number for this computer.
        /// </summary>
        public static uint WinMajorVersion
        {
            get
            {
                dynamic major;
                // The 'CurrentMajorVersionNumber' string value in the CurrentVersion key is new for Windows 10, 
                // and will most likely (hopefully) be there for some time before MS decides to change this - again...
                if (TryGetRegistryKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "CurrentMajorVersionNumber", out major))
                {
                    return (uint) major;
                }

                // When the 'CurrentMajorVersionNumber' value is not present we fallback to reading the previous key used for this: 'CurrentVersion'
                dynamic version;
                if (!TryGetRegistryKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "CurrentVersion", out version))
                    return 0;

                var versionParts = ((string) version).Split('.');
                if (versionParts.Length != 2) return 0;
                uint majorAsUInt;
                return uint.TryParse(versionParts[0], out majorAsUInt) ? majorAsUInt : 0;
            }
        }

        /// <summary>
        ///     Returns the Windows minor version number for this computer.
        /// </summary>
        public static uint WinMinorVersion
        {
            get
            {
                dynamic minor;
                // The 'CurrentMinorVersionNumber' string value in the CurrentVersion key is new for Windows 10, 
                // and will most likely (hopefully) be there for some time before MS decides to change this - again...
                if (TryGetRegistryKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "CurrentMinorVersionNumber",
                    out minor))
                {
                    return (uint) minor;
                }

                // When the 'CurrentMinorVersionNumber' value is not present we fallback to reading the previous key used for this: 'CurrentVersion'
                dynamic version;
                if (!TryGetRegistryKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "CurrentVersion", out version))
                    return 0;

                var versionParts = ((string) version).Split('.');
                if (versionParts.Length != 2) return 0;
                uint minorAsUInt;
                return uint.TryParse(versionParts[1], out minorAsUInt) ? minorAsUInt : 0;
            }
        }

        /// <summary>
        ///     Returns whether or not the current computer is a server or not.
        /// </summary>
        public static uint IsServer
        {
            get
            {
                dynamic installationType;
                if (TryGetRegistryKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "InstallationType",
                    out installationType))
                {
                    return (uint) (installationType.Equals("Client") ? 0 : 1);
                }

                return 0;
            }
        }

        private static bool TryGetRegistryKey(string path, string key, out dynamic value)
        {
            value = null;
            try
            {
                var rk = Registry.LocalMachine.OpenSubKey(path);
                if (rk == null) return false;
                value = rk.GetValue(key);
                return value != null;
            }
            catch
            {
                return false;
            }
        }
    }
}
string subKey = @"SOFTWARE\Wow6432Node\Microsoft\Windows NT\CurrentVersion";
Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.LocalMachine;
Microsoft.Win32.RegistryKey skey = key.OpenSubKey(subKey);

string name = skey.GetValue("ProductName").ToString();
if(name.Contains("Windows 10"))
{
    //... procedures
}
else
{
   //... procedures
}