How can I detect if my app is running on Windows 10
How can I detect if my app is running on Windows 10
I'm looking for a means to detect if my C# app is running on Windows 10.
I had hoped that Environment.OSVersion
would do the trick, but this seems to return a Version
of 6.3.9600.0
on Windows 8.1 and Windows 10.
Other solutions such as this don't seem to distinguish between Windows 8 and Windows 10 either.
Any suggestions?
Edit
Why do I need to do this?
Because I'm using a WinForms WebBrowser control to host an OAuth page that crashes and burns in older IE versions (my app connects to a user's Nest account...).
By default, the WebBrowser control emulates IE7. Using a Registry key, you can tell it to emulate the latest version of IE that is installed on the host PC. However, the value that worked up to Windows 8.1 (and pre-releases of Windows 10) does not work in the final version of Windows 10.
Answer by Rahul for How can I detect if my app is running on Windows 10
Have you tried below? [You need to add a reference to Microsoft.VisulaBasic
dll]
new Microsoft.VisualBasic.Devices.ComputerInfo().OSFullName
On my machine it gives, Microsoft Windows 7 Ultimate
Answer by Mitat Koyuncu for How can I detect if my app is running on Windows 10
If you look at registry you will found environment name:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProductName
For example my product name is Windows 10 Home
:
With this code you get if it 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"); }
Note: Add using Microsoft.Win32;
to your usings.
Answer by sstan for How can I detect if my app is running on Windows 10
Under the hood, Environment.OSVersion uses the GetVersionEx function, which has been deprecated. The documentation warns about the behavior you observed:
Applications not manifested for Windows 8.1 or Windows 10 will return the Windows 8 OS version value (6.2).
The documentation goes on to recommend:
Identifying the current operating system is usually not the best way to determine whether a particular operating system feature is present. This is because the operating system may have had new features added in a redistributable DLL. Rather than using GetVersionEx to determine the operating system platform or version number, test for the presence of the feature itself.
If the above recommendation is not appropriate for your case, and you really want to check the actual running OS version, then the documentation also provides a hint about this:
To compare the current system version to a required version, use the VerifyVersionInfo function instead of using GetVersionEx to perform the comparison yourself.
The following article has posted a working solution using the VerifyVersionInfo function: Version Helper API for .NET.
Giving full credit to the author of that article, the following code snippet should provide the behavior you are looking for:
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); } }
Disclaimer: I don't have Windows 10 yet, so I haven't tested the code on Windows 10.
Answer by Dave Williams for How can I detect if my app is running on Windows 10
Answer
Use Environment.OSVersion
and add an application manifest file with relevant supportedOS
elements uncommented.
Reason
I don't like the answer from @Mitat Koyuncu because is uses the registry unnecessarily and as mentioned in the comments uses unreliable string parsing.
I also don't like the answer from @sstan because it uses third party code and it needs the application manifest anyway.
From MSDN:
Windows 10: VerifyVersionInfo returns false when called by applications that do not have a compatibility manifest for Windows 8.1 or Windows 10 if the lpVersionInfo parameter is set so that it specifies Windows 8.1 or Windows 10, even when the current operating system version is Windows 8.1 or Windows 10. Specifically, VerifyVersionInfo has the following behavior:
? If the application has no manifest, VerifyVersionInfo behaves as if the operation system version is Windows 8 (6.2).
? If the application has a manifest that contains the GUID that corresponds to Windows 8.1, VerifyVersionInfo behaves as if the operation system version is Windows 8.1 (6.3).
? If the application has a manifest that contains the GUID that corresponds to Windows 10, VerifyVersionInfo behaves as if the operation system version is Windows 10 (10.0).
The reason is because VerifyVersionInfo is deprecated in Windows 10.
I have tested on Windows 10 and indeed Environment.OSVersion
works exactly as expected when the app.Manifest contains the relevant GUID as above. That is most likely why they did not change or deprecate it from .Net Framework.
Answer by Spiralis for How can I detect if my app is running on Windows 10
I suggest using the registry to find the values you want. Microsoft has changed the way Windows 10 is listed in the registry so the code needs to adapt for that.
Here is the code I uses, that correctly identifies Windows 10 as well:
namespace Inspection { /// /// Static class that adds convenient methods for getting information on the running computers basic hardware and os setup. /// public static class ComputerInfo { /// /// Returns the Windows major version number for this computer. /// 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 (TryGeRegistryKey(@"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 (!TryGeRegistryKey(@"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; } } /// /// Returns the Windows minor version number for this computer. /// 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 (TryGeRegistryKey(@"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 (!TryGeRegistryKey(@"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; } } /// /// Returns whether or not the current computer is a server or not. /// public static uint IsServer { get { dynamic installationType; if (TryGeRegistryKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "InstallationType", out installationType)) { return (uint) (installationType.Equals("Client") ? 0 : 1); } return 0; } } private static bool TryGeRegistryKey(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; } } } }
Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72
0 comments:
Post a Comment