How to Detect DotNet on Windows and Linux Systems

Detect DotNet Runtime and SDK: Step-by-Step Detection Methods

Understanding which .NET runtimes and SDKs are installed on a machine is essential for troubleshooting, deployment, and ensuring compatibility. This guide shows reliable, repeatable methods for detecting .NET runtimes and SDKs on Windows, macOS, and Linux — including command-line checks, programmatic detection, and automated scripts.

1. Quick overview: runtime vs SDK

  • Runtime: Components needed to run .NET apps (e.g., .NET 6 runtime).
  • SDK: Includes the compiler and tools to build .NET apps (e.g., .NET 6 SDK).
    Both can coexist in multiple versions; detection should check both.

Command-line detection (recommended)

Windows (PowerShell or Command Prompt)

  • Check installed SDKs and runtimes:

    Code

    dotnet –list-sdks dotnet –list-runtimes
    • Output lists versions and install paths (e.g., 6.0.100 [C:\Program Files\dotnet\sdk]).
  • Check overall dotnet availability and version:

    Code

    dotnet –version dotnet –info
    • –info shows environment, installed runtimes, SDKs, and system architecture.

macOS and Linux (Terminal)

  • Same dotnet CLI commands work if .NET is installed via official packages:

    Code

    dotnet –list-sdks dotnet –list-runtimes dotnet –info
  • If dotnet is not found, check typical install locations:
    • Linux: /usr/share/dotnet, /usr/local/share/dotnet
    • macOS: /usr/local/share/dotnet or homebrew locations

Programmatic detection

.NET global.json and runtime roll-forward considerations

  • Projects can pin SDK versions with global.json; presence of global.json affects which SDK dotnet uses.
  • Runtimes are selected at runtime; detecting installed runtimes helps predict app behavior.

Using a script (cross-platform)

  • Minimal script (bash/PowerShell) to capture versions:

Bash (Linux/macOS):

Code

if command -v dotnet >/dev/null 2>&1; then dotnet –list-sdks dotnet –list-runtimes else echo “dotnet not found” fi

PowerShell (Windows/macOS/Linux):

Code

if (Get-Command dotnet -ErrorAction SilentlyContinue) { dotnet –list-sdks dotnet –list-runtimes } else { Write-Output “dotnet not found” }

Detecting .NET from within code

C# (at runtime)

  • To detect the runtime version your application is running on:

csharp

using System; using System.Runtime.InteropServices; Console.WriteLine(RuntimeInformation.FrameworkDescription);
  • To enumerate installed SDKs/runtimes from code, call the dotnet CLI and parse output using Process APIs.

Other languages

  • Shell out to dotnet –list- and parse output (Node.js, Python, etc.). Example (Node.js):

js

const { execSync } = require(‘child_process’); try { console.log(execSync(‘dotnet –list-sdks’).toString()); console.log(execSync(‘dotnet –list-runtimes’).toString()); } catch (e) { console.error(‘dotnet not found’); }

Detecting .NET installations without dotnet CLI

  • On Windows, check registry keys:
    • SDKs: HKEY_LOCAL_MACHINE\SOFTWARE\dotnet\Setup\InstalledVersions\x64\sdk
    • Runtimes: HKEY_LOCAL_MACHINE\SOFTWARE\dotnet\Setup\InstalledVersions\x64\sharedfx
  • On Linux/macOS, inspect installation directories:
    • /usr/share/dotnet/shared (runtimes)
    • /usr/share/dotnet/sdk (sdks)
  • For container images, check image layers or Dockerfile used to build the image (look for mcr.microsoft.com/dotnet/ base images).

Automated inventory across multiple machines

Using configuration management tools

  • Use Ansible, Chef, or PowerShell Remoting to run dotnet –list- on each host and collect results.
  • Example Ansible task:

yaml

- name: Check dotnet SDKs and runtimes shell: dotnet --list-sdks && dotnet --list-runtimes register: dotnet_info

Centralized logging

  • Send collected outputs to a logging/asset system (ELK, Splunk) for querying and alerting if required versions are missing.

Common pitfalls and tips

  • Multiple installations: dotnet CLI may show multiple SDKs/runtimes — use paths shown to determine which are active.
  • global.json can force use of a specific SDK even if newer SDKs are installed.
  • OS package managers (apt, yum, brew) may install versions in different locations; prefer dotnet –list- when available.
  • Container images may not include the SDK; runtime-only images are smaller.

Quick troubleshooting checklist

  1. Run dotnet –info.
  2. If dotnet not found, check PATH and installation directories.
  3. On Windows, verify registry keys under dotnet setup.
  4. For build issues, inspect global.json in project root.
  5. If using containers, ensure correct base image (sdk vs runtime).

Summary

Use the dotnet CLI (–list-sdks, –list-runtimes, –info) as the primary, cross-platform method. Supplement with registry checks on Windows, directory inspection on Linux/macOS, programmatic calls when needed, and centralized collection for large environments.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *