Follow us on X (Twitter)  Like us on Facebook  Connect with us on LinkedIn  Subscribe to our YouTube Channel  Subscribe to our WhatsApp Group

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.

 

Programmatically retrieve Visual Studio install directory (www.kunal-chowdhury.com)

 

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:

 

Registry Path to retrieve Visual Studio installation directory (www.kunal-chowdhury.com)

 

 

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.

 

internal static string GetVisualStudioInstalledPath()
{
    var visualStudioInstalledPath = string.Empty;
    var visualStudioRegistryPath = Registry.LocalMachine.OpenSubKey(
                                   @"SOFTWARE\WOW6432Node\Microsoft\VisualStudio\14.0");
    if (visualStudioRegistryPath != null)
    {
        visualStudioInstalledPath = visualStudioRegistryPath.GetValue("InstallDir", string.Empty) 
                                                                      as string;
    }
 
    if(string.IsNullOrEmpty(visualStudioInstalledPath) || 
                            !Directory.Exists(visualStudioInstalledPath))
    {
        visualStudioRegistryPath = Registry.LocalMachine.OpenSubKey(
                                   @"SOFTWARE\Microsoft\VisualStudio\14.0");
        if (visualStudioRegistryPath != null)
        {
            visualStudioInstalledPath = visualStudioRegistryPath.GetValue("InstallDir", 
                                                                          string.Empty) as string;
        }
    }
 
    if (string.IsNullOrEmpty(visualStudioInstalledPath) || 
                             !Directory.Exists(visualStudioInstalledPath))
    {
        visualStudioRegistryPath = Registry.LocalMachine.OpenSubKey(
                                   @"SOFTWARE\WOW6432Node\Microsoft\VisualStudio\12.0");
        if (visualStudioRegistryPath != null)
        {
            visualStudioInstalledPath = visualStudioRegistryPath.GetValue("InstallDir", 
                                                                          string.Empty) as string;
        }
    }
 
    if (string.IsNullOrEmpty(visualStudioInstalledPath) || 
                             !Directory.Exists(visualStudioInstalledPath))
    {
        visualStudioRegistryPath = Registry.LocalMachine.OpenSubKey(
                                   @"SOFTWARE\Microsoft\VisualStudio\12.0");
        if (visualStudioRegistryPath != null)
        {
            visualStudioInstalledPath = visualStudioRegistryPath.GetValue("InstallDir", 
                                                                          string.Empty) as string;
        }
    }
 
    return visualStudioInstalledPath;
}

 

 

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.

 

 

Have a question? Or, a comment? Let's Discuss it below...

Thank you for visiting our website!

We value your engagement and would love to hear your thoughts. Don't forget to leave a comment below to share your feedback, opinions, or questions.

We believe in fostering an interactive and inclusive community, and your comments play a crucial role in creating that environment.