Dim*_*_Ka 23
我使用此方法查找Visual Studio 2010的安装路径:
private string GetVisualStudioInstallationPath()
{
string installationPath = null;
if (Environment.Is64BitOperatingSystem)
{
installationPath = (string)Registry.GetValue(
"HKEY_LOCAL_MACHINE\\SOFTWARE\\Wow6432Node\\Microsoft\\VisualStudio\\10.0\\",
"InstallDir",
null);
}
else
{
installationPath = (string)Registry.GetValue(
"HKEY_LOCAL_MACHINE\\SOFTWARE \\Microsoft\\VisualStudio\\10.0\\",
"InstallDir",
null);
}
return installationPath;
}
Run Code Online (Sandbox Code Playgroud)
Kev*_*ler 10
我建议查询注册表以获取此信息.这给出了实际的安装目录,而不需要组合路径,它也适用于快速版本.这可能是一个重要的区别,具体取决于您需要做什么(例如,模板根据Visual Studio的版本安装到不同的目录).注册表位置如下(请注意,Visual Studio是一个32位程序,将安装到x64计算机上的注册表的32位部分):
其中Major是主要版本号,Minor是次要版本号,冒号后面的文本是注册表值的名称.例如,Visual Studio 2008 Professional的安装目录将位于InstallLir值中的HKLM\SOFTWARE\Microsoft\Visual Studio\9.0密钥中.
这是一个代码示例,它打印几个版本的Visual Studio和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());
}
Run Code Online (Sandbox Code Playgroud)
Visual Studio的非快速版本也编写了一个可以检查的环境变量,但它给出了常用工具目录的位置,而不是安装目录,因此您必须进行一些路径组合.环境变量的格式为VS*COMNTOOLS,其中*是主要版本号和次要版本号.例如,Visual Studio 2010的环境变量是VS100COMNTOOLS,并包含类似C:\ Program Files\Microsoft Visual Studio 10.0\Common7\Tools的值.
下面是一些示例代码,用于为多个版本的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"));
}
Run Code Online (Sandbox Code Playgroud)
环境:感谢Zeb和Sam提出的VS*COMNTOOLS环境变量建议.要在PowerShell中访问IDE:
$vs = Join-Path $env:VS90COMNTOOLS '..\IDE\devenv.exe'
Run Code Online (Sandbox Code Playgroud)
注册表:看起来像注册表位置HKLM\Software\Microsoft\VisualStudio,每个安装都有特定于版本的子项.在PowerShell中:
$vsRegPath = 'HKLM:\Software\Microsoft\VisualStudio\9.0'
$vs = (Get-ItemProperty $vsRegPath).InstallDir + 'devenv.exe'
Run Code Online (Sandbox Code Playgroud)
[改编自此处 ]
| 归档时间: |
|
| 查看次数: |
22674 次 |
| 最近记录: |