Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I detect if my app is running on Windows 10

Tags:

c#

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?


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.

like image 874
Richard Ev Avatar asked Aug 07 '15 19:08

Richard Ev


People also ask

How do you tell what's running in the background on my computer?

#1: Press "Ctrl + Alt + Delete" and then choose "Task Manager". Alternatively you can press "Ctrl + Shift + Esc" to directly open task manager. #2: To see a list of processes that are running on your computer, click "processes". Scroll down to view the list of hidden and visible programs.

What programs are running in the background?

Take a look in Settings > Network & Internet > Data Usage and tap any app to view its Background Data.


2 Answers

Answer

Use Environment.OSVersion and add an application manifest file with relevant supportedOS elements uncommented.

e.g. add this under <asmv1:assembly>

<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">      <application>          <!-- Windows 10 -->          <supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}"/>         <!-- Windows 8.1 -->         <supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}"/>         <!-- Windows Vista -->         <supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}"/>          <!-- Windows 7 -->         <supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}"/>         <!-- Windows 8 -->         <supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}"/>     </application>  </compatibility> 

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.

like image 183
Dave Williams Avatar answered Oct 09 '22 18:10

Dave Williams


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:

Registry

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.

like image 30
Mitat Koyuncu Avatar answered Oct 09 '22 16:10

Mitat Koyuncu