Programmatically retrieve Visual Studio install di

2020-02-09 06:44发布

I know there is a registry key indicating the install directory, but I don't remember what it is off-hand.

I am currently interested in Visual Studio 2008 install directory, though it wouldn't hurt to list others for future reference.

14条回答
狗以群分
2楼-- · 2020-02-09 07:24

It is a real problem that all Visual Studio versions have their own location. So the solutions here proposed are not generic. However, Microsoft has made a utility available for free (including the source code) that solved this problem (i.e. annoyance). It is called vswhere.exe and you can download it from here. I am very happy with it, and hopefully it will also do for future releases. It makes the whole discussion on this page redundant.

查看更多
Emotional °昔
3楼-- · 2020-02-09 07:25

Environment: Thanks to Zeb and Sam for the VS*COMNTOOLS environment variable suggestion. To get to the IDE in PowerShell:

$vs = Join-Path $env:VS90COMNTOOLS '..\IDE\devenv.exe'

Registry: Looks like the registry location is HKLM\Software\Microsoft\VisualStudio, with version-specific subkeys for each install. In PowerShell:

$vsRegPath = 'HKLM:\Software\Microsoft\VisualStudio\9.0'
$vs = (Get-ItemProperty $vsRegPath).InstallDir + 'devenv.exe'

[Adapted from here]

查看更多
Luminary・发光体
4楼-- · 2020-02-09 07:26

Registry Method

I recommend querying the registry for this information. This gives the actual installation directory without the need for combining paths, and it works for express editions as well. This could be an important distinction depending on what you need to do (e.g. templates get installed to different directories depending on the edition of Visual Studio). The registry locations are as follows (note that Visual Studio is a 32-bit program and will be installed to the 32-bit section of the registry on x64 machines):

  • Visual Studio: HKLM\SOFTWARE\Microsoft\Visual Studio\Major.Minor:InstallDir
  • Visual C# Express: HKLM\SOFTWARE\Microsoft\VCSExpress\Major.Minor:InstallDir
  • Visual Basic Express: HKLM\SOFTWARE\Microsoft\VBExpress\Major.Minor:InstallDir
  • Visual C++ Express: HKLM\SOFTWARE\Microsoft\VCExpress\Major.Minor:InstallDir

where Major is the major version number, Minor is the minor version number, and the text after the colon is the name of the registry value. For example, the installation directory of Visual Studio 2008 Professional would be located at the HKLM\SOFTWARE\Microsoft\Visual Studio\9.0 key, in the InstallDir value.

Here's a code example that prints the installation directory of several versions of Visual Studio and Visual C# Express:

string visualStudioRegistryKeyPath = @"SOFTWARE\Microsoft\VisualStudio";
string visualCSharpExpressRegistryKeyPath = @"SOFTWARE\Microsoft\VCSExpress";

List<Version> vsVersions = new List<Version>() { new Version("10.0"), new Version("9.0"), new Version("8.0") };
foreach (var version in vsVersions)
{
    foreach (var isExpress in new bool[] { false, true })
    {
        RegistryKey registryBase32 = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32);
        RegistryKey vsVersionRegistryKey = registryBase32.OpenSubKey(
            string.Format(@"{0}\{1}.{2}", (isExpress) ? visualCSharpExpressRegistryKeyPath : visualStudioRegistryKeyPath, version.Major, version.Minor));
        if (vsVersionRegistryKey == null) { continue; }
        Console.WriteLine(vsVersionRegistryKey.GetValue("InstallDir", string.Empty).ToString());
    }

Environment Variable Method

The non-express editions of Visual Studio also write an environment variable that you could check, but it gives the location of the common tools directory, not the installation directory, so you'll have to do some path combining. The format of the environment variable is VS*COMNTOOLS where * is the major and minor version number. For example, the environment variable for Visual Studio 2010 is VS100COMNTOOLS and contains a value like C:\Program Files\Microsoft Visual Studio 10.0\Common7\Tools.

Here's some example code to print the environment variable for several versions of Visual Studio:

List<Version> vsVersions = new List<Version>() { new Version("10.0"), new Version("9.0"), new Version("8.0") };
foreach (var version in vsVersions)
{
    Console.WriteLine(Path.Combine(Environment.GetEnvironmentVariable(string.Format("VS{0}{1}COMNTOOLS", version.Major, version.Minor)), @"..\IDE"));
}
查看更多
Emotional °昔
5楼-- · 2020-02-09 07:29

Use Environment.GetEnvironmentVariable("VS90COMNTOOLS");.

Also in a 64-bit environment, it works for me.

查看更多
地球回转人心会变
6楼-- · 2020-02-09 07:32

You can read the VSINSTALLDIR environment variable.

查看更多
Emotional °昔
7楼-- · 2020-02-09 07:35

For Visual Studio 2017 and Visual Studio 2019 there is the Setup API from Microsoft.

In C#, just add the NuGet package "Microsoft.VisualStudio.Setup.Configuration.Interop", and use it in this way:

    try {
        var query = new SetupConfiguration();
        var query2 = (ISetupConfiguration2)query;
        var e = query2.EnumAllInstances();

        var helper = (ISetupHelper)query;

        int fetched;
        var instances = new ISetupInstance[1];
        do {
            e.Next(1, instances, out fetched);
            if (fetched > 0)
                Console.WriteLine(instances[0].GetInstallationPath());
        }
        while (fetched > 0);
        return 0;
    }
    catch (COMException ex) when (ex.HResult == REGDB_E_CLASSNOTREG) {
        Console.WriteLine("The query API is not registered. Assuming no instances are installed.");
        return 0;
    }

You can find more samples for VC, C#, and VB here.

查看更多
登录 后发表回答