pgee70
pgee70

Reputation: 3984

System.Environment.OSVersion returns wrong version

Using windows 10, upgraded from windows 8 => 8.1 => 10 When I use this code.

OperatingSystem os = System.Environment.OSVersion;

The os.Version = {6.2.9200.0} System.Version

I read this was because of the version it was manifested for but I do not understand what that means.

I want the correct OS version because I am logging a user agent string on a web service, and want to correctly identify the windows version for support. what is the easiest way to get that to correctly report the correct version?

Upvotes: 40

Views: 18412

Answers (5)

sam
sam

Reputation: 57

C# Determine OS Version in .NET 6

RuntimeInformation.OSDescription Property

I used

var osVersion = RuntimeInformation.OSDescription.ToString();

I found it easier than the app.manifest solution for my purpose

Upvotes: 1

alanthinker
alanthinker

Reputation: 53

For dotnet 5.0 and above, System.Environment.OSVersion can return right OS version.

Upvotes: 1

Tal Aloni
Tal Aloni

Reputation: 1519

Another alternative is

Microsoft.DotNet.PlatformAbstractions.RuntimeEnvironment.OperatingSystem + " " + Microsoft.DotNet.PlatformAbstractions.RuntimeEnvironment.OperatingSystemVersion

Microsoft.DotNet.PlatformAbstractions NuGet package reference is needed

Upvotes: 2

TazAstroSpacial
TazAstroSpacial

Reputation: 131

The sequence of steps given by Nasreddine will add a manifest file, into your project.

There is another way to add a manifest. Right click project > properties > Application tab > View Windows Settings This will bring up the default manifest, which you can edit. I am using visual studio 2010 Express. From other reading the method should be similar.

For a project that uses non-windows components (I program in ESRI arc-objcets with VB.Net) there seems to be no way of adding a manifest file. The View Windows Settings button is disabled. The steps given by Nasreddine do add a Manifest file which can be edited but this cannot be incorporated into the project. I have tried many tricks: like adding as a resource (embedded and not embedded), adding it the obj > debug folder.

The kicker test was I set up a pure windows project, got a windo button comand to return the correct version string (after adding a manifest) then tried to add an non-windows component (ESRI Addin). this failed with an error message.

I am was just trying to identify the OS because the file location of required files is dependent on operating system. Hope this save others from going down this rabbit hole.

Upvotes: 2

Nasreddine
Nasreddine

Reputation: 37798

Windows 10 returns that string unless you declare that your application is compatible using a manifest. To do so add an app.manifest (right click your project -> Add -> New Item -> Application Manifest File) then uncomment the following line:

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

You can do the same thing for Windows Vista to Windows 10. All are in the same section:

<application>
  <!-- A list of the Windows versions that this application has been tested on and is
       is designed to work with. Uncomment the appropriate elements and Windows will 
       automatically selected the most compatible environment. -->

  <!-- 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}" />-->

  <!-- Windows 8.1 -->
  <!--<supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}" />-->

  <!-- Windows 10 -->
  <supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />

</application>

And now when you run your application it'll report the correct 10.0.*.0 version

Upvotes: 45

Related Questions