查找Visual Studio安装路径

6

我需要帮助找到Microsoft Visual Studio安装路径,以便在我的程序中使用。有哪个函数可以调用以获取Microsoft Visual Studio安装路径?

6个回答

6

根据应用程序的不同,最好还是询问用户,但以下 C# 代码应该可以在 VS2008 中完成。

RegistryKey regKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\VisualStudio\9.0\Setup\VS");
string vsInstallationPath = regKey.GetValue("ProductDir").ToString();
regKey.Close();

6

可能可以通过搜索注册表来找到它,但由于我想要一个适用于构建脚本的解决方案,因此一直在使用环境变量来完成这个任务。

注意:查询环境变量的名称是与版本相关的。

对于VS2005,您可以使用VS80COMNTOOLS。

对于VS2008,您可以使用VS90COMNTOOLS。

如果您在命令提示符下键入SET VS90COMNTOOLS,则应该看到:

VS90COMNTOOLS=C:\Program Files\Microsoft Visual Studio 9.0\Common7\Tools\

所以向上移动两个文件夹即可到达安装路径的根目录。


4

这是快速了解VS或其他程序安装文件夹的方法。

打开VS Code并运行,打开Windows任务管理器,切换到详细信息选项卡。

右键点击现在应该正在运行的Code.exe应用程序,并选择打开文件位置选项:

Windows任务管理器 > 详细信息选项卡 > 右键点击Code.exe > 打开文件位置

输入图片说明


OP 是在问 Visual Studio,而不是 VSCode。 - undefined

1

对于较新版本的VS,最好使用微软提供的API,因为安装信息不再正确地维护在注册表中。

  1. 安装Nuget包Microsoft.VisualStudio.Setup.Configuration.Native

  2. 使用以下代码获取所有Visual Studio实例的版本和路径:

     private const int REGDB_E_CLASSNOTREG = unchecked((int)0x80040154);
    
     public static IEnumerable<(string, string)> GetVisualStudioInstallPaths() 
     {
         var result = new List<(string, string)>();
    
         try
         {
             var query = new SetupConfiguration() as ISetupConfiguration2;
             var e = query.EnumAllInstances();
    
             int fetched;
             var instances = new ISetupInstance[1];
    
             do
             {
                 e.Next(1, instances, out fetched);
    
                 if (fetched > 0)
                 {
                     var instance2 = (ISetupInstance2)instances[0];
                     result.Add((instance2.GetInstallationVersion(), instance2.GetInstallationPath()));
                 }
             }
             while (fetched > 0);
         }
         catch (COMException ex) when (ex.HResult == REGDB_E_CLASSNOTREG)
         {
         }
         catch (Exception)
         {
         }
    
         return result;
     }
    

此致敬礼


1

从注册表中,HKLM\Software\Microsoft\VisualStudio\9.0\InstallDir 是 Visual Studio 2008 的安装目录。


0

这是为Visual Studio的某个插件吗?

否则,您需要知道,运行您的程序的人可能实际上没有安装Visual Studio。

如果已经安装,您通常可以在注册表的已知位置找到它,例如VS2008的HKCR / Applications / devenv.exe / shell / edit / command


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接