Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 459 Vote(s) - 3.44 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Determine Operating System in .NET Core

#1
How can I determine which operating system my .NET Core app is running on?
In the past I could use `Environment.OSVersion`.

What is the current way to determine whether my app is running on Mac or Windows?
Reply

#2
`System.Environment.OSVersion.Platform` can be used in full .NET Framework and Mono but:

- Mac OS X detection almost never worked for me under Mono
- it is not implemented in .NET Core

`System.Runtime.InteropServices.RuntimeInformation` can be used in .NET Core but:

- it is not implemented in full .NET Framework and Mono
- it does not perform platform detection in runtime but **uses hardcoded information instead**
(see [corefx issue #3032](

[To see links please register here]

) for more details)

You could pinvoke platform specific unmanaged functions such as `uname()` but:

- it may cause segmentation fault on unknown platforms
- is not allowed in some projects

So my suggested solution (see code bellow) may look sily at first but:

- it uses 100% managed code
- it works in .NET, Mono and .NET Core
- it works like a charm so far in [Pkcs11Interop](

[To see links please register here]

) library


<!-- language: lang-cs -->

string windir = Environment.GetEnvironmentVariable("windir");
if (!string.IsNullOrEmpty(windir) && windir.Contains(@"\") && Directory.Exists(windir))
{
_isWindows = true;
}
else if (File.Exists(@"/proc/sys/kernel/ostype"))
{
string osType = File.ReadAllText(@"/proc/sys/kernel/ostype");
if (osType.StartsWith("Linux", StringComparison.OrdinalIgnoreCase))
{
// Note: Android gets here too
_isLinux = true;
}
else
{
throw new UnsupportedPlatformException(osType);
}
}
else if (File.Exists(@"/System/Library/CoreServices/SystemVersion.plist"))
{
// Note: iOS gets here too
_isMacOsX = true;
}
else
{
throw new UnsupportedPlatformException();
}

Reply

#3
## Method

System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform()

## Possible Argument

OSPlatform.Windows
OSPlatform.OSX
OSPlatform.Linux

## Example

bool isWindows = System.Runtime.InteropServices.RuntimeInformation
.IsOSPlatform(OSPlatform.Windows);


## Update

Thanks to the comment by Oleksii Vynnychenko

You can get the operating systems name and version as a string using

var osNameAndVersion = System.Runtime.InteropServices.RuntimeInformation.OSDescription;

E.g. `osNameAndVersion` would be `Microsoft Windows 10.0.10586`
Reply

#4
Check `System.OperatingSystem` class it has static methods for each OS i.e. `IsMacOS()`, `IsWindows()`, `IsIOS()` and so on. These methods are available starting with .NET 5.

This makes it a great choice because the implementations for these methods use preprocessor directives to fix the return value to a constant true/false at compilation time for each target OS the `OperatingSystem` class is compiled for. There is no runtime probing or calls to make.

Here is an excerpt from one such method in `OperatingSystem`:

```c#
/// <summary>
/// Indicates whether the current application is running on Linux.
/// </summary>
[NonVersionable]
public static bool IsLinux() =>
#if TARGET_LINUX && !TARGET_ANDROID
true;
#else
false;
#endif
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through