Andi
Andi

Reputation: 3498

Determine runtime target (OS) in .NET Core app

My .NET Core 3.0 app is published for different operating systems, using the commands dotnet publish -r win10-x64 or dotnet publish -r ubuntu.18.04-x64 for example.

During runtime, in my C# code I want to find out the target the app was built for. I do not mean just the general operating system like Windows or Linux (as asked here), but the exact runtime target, like ubuntu-18.04-x64.

I already found out, that there is a file <AssemblyName>.deps.json. It contains the property "runtimeTarget": { "name": ".NETCoreApp,Version=v3.0/ubuntu.18.04-x64", ..., but maybe there is a better way?

Upvotes: 7

Views: 3406

Answers (2)

Andi
Andi

Reputation: 3498

Since I found no other way, I am using the value found in the .deps.json file. Here is my code:

using Newtonsoft.Json.Linq;
using System;
using System.IO;

/// <summary>
/// Returns the current RID (Runtime IDentifier) where this applications runs.
/// See https://learn.microsoft.com/en-us/dotnet/core/rid-catalog for possible values, e.g. "ubuntu.18.04-x64".
/// The value is read from the first found .deps.json file in the application folder, at the path
/// "runtimeTarget"/"name" the value behind the last "/".
/// When the file or the value behind the last "/" is missing, this application folder was not compiled
/// for a specific runtime, and null is returned.
/// </summary>
public static string? GetRuntimeIdentifier() {
    try {
        // Find first (and probably only) .deps.json file in the application's folder.
        var dir = AppDomain.CurrentDomain.BaseDirectory;
        var files = Directory.GetFiles(dir, "*.deps.json");
        if (files.Length == 0)
            return null;
        // Read JSON content
        var json = JObject.Parse(File.ReadAllText(Path.Combine(dir, files[0])));
        var name = json["runtimeTarget"]["name"].ToString();
        // Read RID after slash
        var slashPos = name.LastIndexOf('/');
        if (slashPos == -1)
            return null;
        return name.Substring(slashPos + 1);
    }
    catch {
        // Unexpected file format or other problem
        return null;
    }
}

Upvotes: 0

Amogh Sarpotdar
Amogh Sarpotdar

Reputation: 617

I am using the code given below with .Net core version 2 (and 1.2 in the past) -

    public static void PrintTargetRuntime()
    {
            var framework = Assembly
                    .GetEntryAssembly()?
                    .GetCustomAttribute<TargetFrameworkAttribute>()?
                    .FrameworkName;

            var stats = new
        {
            OsPlatform = System.Runtime.InteropServices.RuntimeInformation.OSDescription,
            OSArchitecture = System.Runtime.InteropServices.RuntimeInformation.OSArchitecture,
            ProcesArchitecture = System.Runtime.InteropServices.RuntimeInformation.ProcessArchitecture,
            FrameworkDescription = System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription,
            AspDotnetVersion = framework
        };

        Console.WriteLine("Framework version is " + framework);
        Console.WriteLine("OS Platform is : " + stats.OsPlatform );
        Console.WriteLine("OS Architecture is : " + stats.OSArchitecture);
        Console.WriteLine("Framework description is " + stats.FrameworkDescription);
        Console.WriteLine("ASPDotNetVersion is " + stats.AspDotnetVersion);

        if (stats.ProcesArchitecture == Architecture.Arm)
        {
            Console.WriteLine("ARM process.");
        }
        else if (stats.ProcesArchitecture == Architecture.Arm64)
        {
            Console.WriteLine("ARM64 process.");
        }
        else if (stats.ProcesArchitecture == Architecture.X64)
        {
            Console.WriteLine("X64 process.");
        }
        else if (stats.ProcesArchitecture == Architecture.X86)
        {
            Console.WriteLine("x86 process.");
        }
    }

I have tested this on Windows 10 and MacOS Mojave. This comes from here - https://weblog.west-wind.com/posts/2018/Apr/12/Getting-the-NET-Core-Runtime-Version-in-a-Running-Application

On my windows machine the output looks as below - Image displaying version output of code above

Upvotes: 4

Related Questions