Share via

How to get absolute trusted path to dotnet.exe that is installed with Visual Studio installation?

Sajith 86 Reputation points
2026-05-14T11:19:41.3833333+00:00

I have a requirement of launching dotnet.exe with absolute path from a Visual Studio extension. Absolute path is required to avoid CWE-427 risk.

In my machine dotnet.exe is at 'C:\Program Files\dotnet\dotnet.exe' but this location is not always guaranteed and could not find a definite way to get location of dotnet.exe from the extension.

There is a dotnet.exe in 'C:\Program Files\Microsoft Visual Studio\2022\Professional\dotnet\net8.0\runtime\dotnet.exe' in my VS 2022 installation. Can I use this to launch the process? But for this also, how to get absolute trusted path to this? Is there an environment variable that is available to Visual Studio extension that can be used to get the absolute path of dotnet.exe?

Developer technologies | Visual Studio | Extensions

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 84,061 Reputation points
    2026-05-14T19:25:08.4633333+00:00

    As you don’t know where the user installed the official dot sdk, you would need to do a path search.

    public static string FindDotNetPath()
    {
        // Search for 'dotnet.exe' in the directories listed in the PATH environment variable
        var pathEnv = Environment.GetEnvironmentVariable("PATH");
        var paths = pathEnv?.Split(Path.PathSeparator);
    
        return paths?.Select(p => Path.Combine(p, "dotnet.exe"))
                     .FirstOrDefault(File.Exists);
    }
    
    

    But this is no better than letting the os find the .exe. If you really need to know the users official install, then your extension needs a setting that the user enters the path to sdk.

    Was this answer helpful?

    0 comments No comments

  2. Varsha Dundigalla(INFOSYS LIMITED) 4,945 Reputation points Microsoft External Staff
    2026-05-14T13:12:26.9333333+00:00

    Thank you for reaching out.

    From my understanding, the recommendation is based on how Visual Studio and .NET are designed.

    Visual Studio installs and manages its own copy of .NET, separate from the system-wide installation. Because of this, multiple dotnet.exe locations can exist on a machine.

    However, the path of dotnet.exe inside the Visual Studio installation is not a documented or stable contract for extensions. It can vary depending on Visual Studio version, edition, or custom install path, and there is no supported API or environment variable that exposes this path.

    Because of this, relying on the Visual Studio internal dotnet.exe or hardcoding that path is not considered reliable.

    So instead, I suggest avoiding Visual Studio-specific paths and using a known trusted installation (like system-wide .NET) or a proper discovery mechanism. This aligns better with your requirement to use an absolute path while also avoiding CWE‑427 risks.

    Please let us know if you require any further assistance, we’re happy to help. If you found this information useful, kindly mark this as "Accept Answer". So that others in the community facing similar issues can easily find the solution. Your contribution is highly appreciated.

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.