Sometime we need to retrieve the Visual Studio installation directory programmatically using the C#/.NET code and there’s no direct API call available to retrieve this information and also you don’t know the exact path where it has been installed.
Every installation of Visual Studio puts a specific registry path to keep track of the installation directory. If you know that, it’s easy to retrieve the information. Let’s discuss on this topic today.
Every installation of Visual Studio puts the information related to installed directory in a specific registry path. It could be either “HKLM\SOFTWARE\Microsoft\VisualStudio\[Major.Minor]\InstallDir” or “HKML\SOFTWARE\WOW6432Node\Microsoft\VisualStudio\[Major.Minor]\InstallDir”, based on 32-bit or 64-bit, where [Major.Minor] is the Visual Studio version no. For example, “12.0” denotes Visual Studio 2013 and “14.0” denotes Visual Studio 2015. In this registry path, “InstallDir” is the string value where it stores the exact path of the installation directory. Here is a screenshot of the same, for your reference:
To retrieve the same information programmatically, you have to write a piece of code (will demonstrate using C# here) and have to iterate through the said registry paths to find out the installed directory of the installed Visual Studio instance.
Hope, this gave you basic idea on the topic and you will be able to use it directly or after optimization to get the desired output. Do remember that, the code shared above is not optimized and kept in brief to give you clear picture of the same.
Also, the code shared here checks for installation directory of Visual Studio 2015 and Visual Studio 2013 only. If you want it to retrieve for other Visual Studio instance, give the appropriate version no. as Major.Minor, as mentioned above.