can you help me to write a programme to show on which operating system i'm working now.

Dani AI

Generated

Short answer and context: detecting the OS from code is straightforward, but the correct technique depends on which .NET you target. gave a good first try and / were right to push for learning the underlying APIs. Below is up-to-date, practical guidance that works across platforms and avoids common pitfalls.

Be careful with Environment.OSVersion on Windows: the underlying Win32 version APIs (GetVersion/GetVersionEx/VerifyVersionInfo) changed behavior around Windows 8.1 and later. If an executable is not manifested to declare support for newer Windows releases, those APIs (and by extension Environment.OSVersion) can report older version numbers. If you need exact Windows version numbers, either add the proper compatibility entries in your app manifest or avoid relying on version numbers. See Microsoft guidance on the version APIs and app manifest behavior. (GetVersionEx/manifest guidance) (learn.microsoft.com)

Recommended modern approach: use System.Runtime.InteropServices.RuntimeInformation (and in .NET 5+ the OperatingSystem helper methods). RuntimeInformation.IsOSPlatform lets you branch on Windows/Linux/macOS and RuntimeInformation.OSDescription gives a friendly OS string you can log. The OperatingSystem.IsWindows/IsLinux helpers are convenient where available. Example (cross-platform, modern .NET):

using System;
using System.Runtime.InteropServices;

Console.WriteLine(RuntimeInformation.OSDescription);

if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
    Console.WriteLine("Windows");
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
    Console.WriteLine("Linux");
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
    Console.WriteLine("macOS");

See the RuntimeInformation API and the OperatingSystem helpers for details. (learn.microsoft.com)

Quick troubleshooting / best practices: prefer feature-detection over version checks (check for the API/behavior you need rather than a Windows build number); log RuntimeInformation.OSDescription during testing; add the appropriate <supportedOS> entries to a manifest only when you truly need exact Windows-version semantics; and test on each target OS. Microsoft’s compatibility guidance explains why feature detection is usually the safer route. (Windows compatibility guidance). (learn.microsoft.com)

Recommended Answers

All 4 Replies

Sure - but you have to start it and get some work on your own first. If you're stuck getting started let us know, we can give you some general guidelines but you will probably not find someone who will write the program for you.

Make sure you have read this http://www.daniweb.com/forums/announcement61-2.html
even if it's not homework per se it still applies.

Yep homework or not, we arent a free set of programmers to do everyones coding for them, we volenteer to try and help you sort your own coding dilemas :)

can you help me to write a programme to show on which operating system i'm working now.

{
string str=System.Environment.OSversion.ToString();
Console.WriteLine(str);
}

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.