检测VSPackage的内部的Visual Studio版本 [英] Detect the Visual Studio version inside a VSPackage
本文介绍了检测VSPackage的内部的Visual Studio版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我如何检查/检测其中的Visual Studio版本下,我VSPackage的运行?
How can I check/detect which Visual Studio version is running under my VSPackage?
我无法从注册表中获得,因为计算机可能已经安装了好几个版本,所以我想有一个API,它能够得到它。
I cannot get from the registry because the computer could have several versions installed, so I guess there is an API that is able to get it.
任何人都知道如何从管理的Visual Studio包使用C#怎么做呢?
Anybody knows how to get it from a managed Visual Studio package using C#?
推荐答案
最后,我写了一个类来检测在Visual Studio版本。测试和工作:
Finally I wrote a class to detect the Visual Studio version. Tested and working:
public static class VSVersion
{
static readonly object mLock = new object();
static Version mVsVersion;
static Version mOsVersion;
public static Version FullVersion
{
get
{
lock (mLock)
{
if (mVsVersion == null)
{
string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "msenv.dll");
if (File.Exists(path))
{
FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(path);
string verName = fvi.ProductVersion;
for (int i = 0; i < verName.Length; i++)
{
if (!char.IsDigit(verName, i) && verName[i] != '.')
{
verName = verName.Substring(0, i);
break;
}
}
mVsVersion = new Version(verName);
}
else
mVsVersion = new Version(0, 0); // Not running inside Visual Studio!
}
}
return mVsVersion;
}
}
public static Version OSVersion
{
get { return mOsVersion ?? (mOsVersion = Environment.OSVersion.Version); }
}
public static bool VS2012OrLater
{
get { return FullVersion >= new Version(11, 0); }
}
public static bool VS2010OrLater
{
get { return FullVersion >= new Version(10, 0); }
}
public static bool VS2008OrOlder
{
get { return FullVersion < new Version(9, 0); }
}
public static bool VS2005
{
get { return FullVersion.Major == 8; }
}
public static bool VS2008
{
get { return FullVersion.Major == 9; }
}
public static bool VS2010
{
get { return FullVersion.Major == 10; }
}
public static bool VS2012
{
get { return FullVersion.Major == 11; }
}
}
这篇关于检测VSPackage的内部的Visual Studio版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文