Skip to main content
added 15 characters in body
Source Link

I'm retrieving the operating system platform with .NET Core with system.runtime.interopservices

I have created the code below and was wondering if there was aany other way that I could useimprove it since I haven't been able to get a switch statement rather than the if statement for determining which platform isto work in usethe OperatingSystem class.

I'm retrieving the operating system platform with .NET Core with system.runtime.interopservices

I have created the code below and was wondering if there was a way that I could use a switch statement rather than the if statement for determining which platform is in use.

I'm retrieving the operating system platform with .NET Core with system.runtime.interopservices

I have created the code below and was wondering if there was any other way that I could improve it since I haven't been able to get a switch statement to work in the OperatingSystem class.

Source Link

Retrieving OS Platform .NET Core

I'm retrieving the operating system platform with .NET Core with system.runtime.interopservices

I have created the code below and was wondering if there was a way that I could use a switch statement rather than the if statement for determining which platform is in use.

/// <summary>
/// Operating System Class
/// For Retrieving The OS
/// Platform.
/// AE : 9/08/2018
/// </summary>
class OperatingSystem
{
    /// <summary>
    /// Operating System Platforms.
    /// </summary>
    public enum OSPlatform
    {
        Windows,
        OSX,
        Linux,
        Not_Supported
    }

    /// <summary>
    /// Return Operating System
    /// </summary>
    public static OSPlatform GetPlatform
    {
        // Switch Statement Not Compatitable
        get
        {
            if (RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows))
            {
                return OSPlatform.Windows;
            }
            else if (RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.OSX))
            {
                return OSPlatform.OSX;
            }
            else if (RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Linux))
            {
                return OSPlatform.Linux;
            }
            else
            {
                return OSPlatform.Not_Supported;
            }
        }
    }
}

The class is being called like this,

static void Main()
{
    switch (OperatingSystem.GetPlatform)
    {
        case OperatingSystem.OSPlatform.Windows:
            Console.WriteLine("Goto Windows Class");
            break;
        case OperatingSystem.OSPlatform.OSX:
            Console.WriteLine("Goto OSX Class");
            break;
        case OperatingSystem.OSPlatform.Linux:
            Console.WriteLine("Goto Linux Class");
            break;
        default:
            Console.WriteLine("OS Not Supported");
            break;
        }
        Console.ReadKey();
    }