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
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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.
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
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
*****************************************************************************
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.