.netvisual-studiovsixvspackagevs-extensibility

Detect the Visual Studio version inside a VSPackage


How can I check/detect which Visual Studio version is running under my VSPackage?

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.

Anybody knows how to get it from a managed Visual Studio package using C#?


Solution

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