简体   繁体   English

查找安装 Visual Studio 的路径

[英]FInding the path where Visual Studio is Installed

I need help as to how I can find the path where Microsoft visual Studio is installed.我需要有关如何找到安装 Microsoft visual Studio 的路径的帮助。 I need to use that path in my program.我需要在我的程序中使用该路径。 What is the function that has to be called to get the path where Microsoft Visual Studio is installed?必须调用什么 function 才能获取安装 Microsoft Visual Studio 的路径?

Depending on the app, it's probably best to ask the user, but here's some C# code that should do the trick for VS2008.根据应用程序的不同,最好询问用户,但这里有一些 C# 代码应该可以为 VS2008 提供帮助。

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

It is probably possible to find it by searching the registry, but as I wanted a solution for build scripts I have been using environment variables to do this.可能可以通过搜索注册表来找到它,但是因为我想要一个构建脚本的解决方案,所以我一直在使用环境变量来做到这一点。

NB The name of the environment variable to query is version specific.注意要查询的环境变量的名称是特定于版本的。

For VS2005 you can use VS80COMNTOOLS对于 VS2005,您可以使用 VS80COMNTOOLS

For VS2008 you can use VS90COMNTOOLS对于 VS2008,您可以使用 VS90COMNTOOLS

If you type SET VS90COMNTOOLS at a command prompt you should see: VS90COMNTOOLS=C:\\Program Files\\Microsoft Visual Studio 9.0\\Common7\\Tools\\如果在命令提示符下键入 SET VS90COMNTOOLS,您应该看到: VS90COMNTOOLS=C:\\Program Files\\Microsoft Visual Studio 9.0\\Common7\\Tools\\

so go up two folders to get to the root of the install path.所以向上走两个文件夹以到达安装路径的根目录。

This is the quickest way to know the folder where VS is installed or any other program.这是了解安装 VS 或任何其他程序的文件夹的最快方法。

Open VS code and when it is running;打开 VS 代码和运行时; open the Windows Task Manager and navigate to the Details tab .打开Windows 任务管理器并导航到“详细信息”选项卡

Right-click on the Code.exe application that should be running by now and choose the option to Open File Location :右键单击现在应该运行的 Code.exe 应用程序,然后选择Open File Location选项:

Windows Task Manager > Details Tab > RIGHTCLICK Code.exe > Open File Location Windows 任务管理器>详细信息选项卡> RIGHTCLICK Code.exe >打开文件位置

在此处输入图片说明

从注册表中,HKLM\\Software\\Microsoft\\VisualStudio\\9.0\\InstallDir for Visual Studio 2008

For newer versions of VS it is better to use from Microsoft provided APIs, because install information is no longer maintained in registry correctly.对于较新版本的 VS,最好使用 Microsoft 提供的 API,因为安装信息不再正确维护在注册表中。

  1. install Nuget package Microsoft.VisualStudio.Setup.Configuration.Native安装 Nuget package Microsoft.VisualStudio.Setup.Configuration.Native

  2. do the trick (returned is tuple with version and path of all VS instances):做这个技巧(返回的是包含所有 VS 实例的版本和路径的元组):

     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; }

Regards问候

Is this for an add-in of some sort for Visual Studio?这是用于 Visual Studio 的某种加载项吗?

Because otherwise, you need to be aware that someone running your program may not actually have Visual Studio installed.因为否则的话,你需要知道有人运行您的程序可能实际上并没有安装Visual Studio。

If it is installed, you can generally find it at a known location in the registry, such as HKCR/Applications/devenv.exe/shell/edit/command for VS2008.如果安装了它,你通常可以在注册表中的一个已知位置,如发现HKCR/Applications/devenv.exe/shell/edit/command为VS2008。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM