How to get linux OS name, version and Username in c# .net core application

BR Kiran 20 Reputation points
2023-10-20T05:50:31.2833333+00:00

My application is running on windows as well as in linux machine.

I'm using below properties,

'System.Runtime.InteropServices.RuntimeInformation.OSDescription' to fetch OS name

'Environment.OSVersion.Version' to fetch OS version

'Environment.UserName' to fetch user name

in windows I'm getting values as expected but in linux I'm not getting the expected values.

below are the values i'm getting,

windows,

OS name : Microsoft Windows 10.0.19045

OS version: 10.0.19045

Username: TestUser

Linux,

OS name : Linux 4.18.0-372.9.1.el8.x86_64 #1 SMP Fri Apr 15 22:12:19 EDT 2022

OS version: 4.18.0.372

Username: root

expected values for linux,

OS name : RHEL

OS version: 8.8

Username: TestUser

please help me to get expected values in linux machine.

Developer technologies ASP.NET ASP.NET Core
Developer technologies .NET Other
Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2023-10-20T16:22:20.0566667+00:00

    typically the o/s info is in the file:

    /etc/os-release

    if the user is root, then the program is running under the superuser. to get the user name use the environment variable USER, or system() the utility whoami


1 additional answer

Sort by: Most helpful
  1. Anonymous
    2023-10-23T13:32:12.0066667+00:00

    Hi @BR Kiran ,

    There's no good way to get linux friendly os name for now. As Bruce suggested, you could try to read the "/etc/os-release" file.

    var osName = string.Empty;
    var osVersion  = string.Empty;
    
    string filePath = "/etc/os-release";
    
    using (FileStream fileStream = new FileStream(filePath, FileMode.Open))
    {
        using (StreamReader reader = new StreamReader(fileStream))
        {
            string line;
    
            while ((line = reader.ReadLine()) != null)
            {
    
                if (line.StartsWith("NAME"))
                {
                    osName = line.Split('\"')[1]; //split the line string by " and get the second slice
                }
    
                if (line.StartsWith("VERSION_ID"))
                {
                    osVersion = line.Split('\"')[1];
                }
            }
        }
    }
    Console.WriteLine("OS name: "+osName);
    Console.WriteLine("OS version: "+osVersion);
    

    OUTPUT

    User's image

    ***************************************************************************** 

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.