2 Replies Latest reply on Jul 5, 2016 3:19 PM by donschenck

    Is there a way to determine the operating system from within a C# program?

    tnolan

      Hi there. I need to know if my code is running in Windows or Mac or Linux. Is there a way to determine the operating system within a C# program?

        • 1. Re: Is there a way to determine the operating system from within a C# program?
          saravanan.subramaniam

          using System;

          class Sample

          {

             public static void Main()

            {

            Console.WriteLine();

            Console.WriteLine("OSVersion: {0}", Environment.OSVersion.ToString());

            }

          }

          /*
          This example produces the following results:
          OSVersion: Microsoft Windows NT 5.1.2600.0
          */

          • 2. Re: Is there a way to determine the operating system from within a C# program?
            donschenck

            The above code works well in Windows, but in RHEL it throws the error "error CS0117: 'Environment' does not contain a definition for 'OSVersion'"

             

            The following code works with any operating system:

             

            using System;

            using System.Runtime.InteropServices;

             

             

            namespace ConsoleApplication

            {

                public class Program

                {

                    public static void Main(string[] args)

                    {

                        Console.WriteLine(System.Runtime.InteropServices.RuntimeInformation.OSDescription.ToString());

                        Console.WriteLine(System.Runtime.InteropServices.RuntimeInformation.OSArchitecture.ToString());

                    }

                }

            }