检测VSPackage中的Visual Studio版本

Dan*_*lba 13 .net vspackage visual-studio vs-extensibility vsix

如何检查/检测在我的VSPackage下运行的Visual Studio版本?

我无法从注册表中获取,因为计算机可能安装了多个版本,所以我猜有一个能够获得它的API.

有人知道如何使用C#从托管的Visual Studio包中获取它吗?

Dmi*_*lov 15

您可以尝试通过自动化DTE对象获取版本.在MPF中你可以这样得到它:

EnvDTE.DTE dte = (EnvDTE.DTE)Package.GetGlobalService(typeof(EnvDTE.DTE));
Run Code Online (Sandbox Code Playgroud)

还有一些其他相关的东西可以检索DTE对象 - 通过Project.DTE,如果你为DTE获取null ,也会读取这个线程.

然后,您可以使用DTE.Version属性获取该版本.

也可以在Carlos Quintero(VS addin ninja)网站上找到有用的信息HOWTO:检测已安装的Visual Studio版本,软件包或服务包


Dan*_*lba 9

最后我写了一个类来检测Visual Studio版本.经过测试和工作:

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; }
    }
}
Run Code Online (Sandbox Code Playgroud)