3

I'm using the WPF application. I want to check programmatically whether .Net SDK is installed or not. I'm getting the list of SDK using the below code. But the problem is registry may change in the future.

RegistryKey ndpKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\WOW6432Node\dotnet\Setup\InstalledVersions\x64\sdk");
var names = ndpKey.GetValueNames();

I also run the following code but it gives me a .NET runtime version.

var netCoreVersion = Environment.Version; 
var runtimeVersion= RuntimeInformation.FrameworkDescription;

Is there any way programmatically I can find whether .Net SDK is installed or not.

Faisal Sajjad
  • 239
  • 2
  • 4
  • 13
  • I've got to ask, _why_ do you want to know what SDKs are installed from within your program? Anyone dealing with SDKs can run the `dotnet --list-sdks` command and get a list. Checking available runtimes is a far more common scenario. – Logarr Feb 02 '22 at 18:28
  • @Logarr I have to build a .NET project programmatically that's why I want to make sure SDK is installed before triggering the build. – Faisal Sajjad Feb 02 '22 at 18:32
  • 1
    In that case I suggest you look into how to execute the `dotnet` command line tool and parse its output. You'll also have to make sure and handle situations where that tool is not present. – Logarr Feb 02 '22 at 18:39

2 Answers2

1

Is there any way programmatically I can find whether .Net SDK is installed or not.

There is no API do to this so either stick with your approach of looking in the registry or invoke the dotnet command-line tool programmatically and capture its output:

Process process = new Process();
process.StartInfo.FileName = "dotnet.exe";
process.StartInfo.Arguments = "--version";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.Start();

string version = process.StandardOutput.ReadToEnd()?.TrimEnd();

process.WaitForExit();

You probably also want to catch exceptions to handle cases where the tool isn't installed.

mm8
  • 163,881
  • 10
  • 57
  • 88
-2

From your code, you can do this.

var netCoreVer = System.Environment.Version;
var runtimeVer = System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription;
Simisola
  • 9
  • 1
  • 3
    This appears to be literally what the OP has as an example of what _doesn't_ work for them. The only difference is you included the full namespaces. – Logarr Feb 02 '22 at 18:24