16

VSPackageで実行されているVisualStudioのバージョンを確認/検出するにはどうすればよいですか?

コンピューターに複数のバージョンがインストールされている可能性があるため、レジストリから取得できません。そのため、レジストリを取得できるAPIがあると思います。

C#を使用してマネージドVisual Studioパッケージから取得する方法を知っている人はいますか?

4

6 に答える 6

17

オートメーション DTE object を介してバージョンを取得しようとすることができます。MPF では、次の方法で取得できます。

EnvDTE.DTE dte = (EnvDTE.DTE)Package.GetGlobalService(typeof(EnvDTE.DTE));

DTE オブジェクトを取得するための関連事項が他にもいくつかあります - Project.DTEを介して、 DTE に対して null を取得している場合は、このスレッドもお読みください。

次に、DTE.Version プロパティを使用してバージョンを取得できます。

また、Carlos Quintero (VS addin ninja) の Web サイトHOWTO: Detect installed Visual Studio editions, packages or service packs にも役立つ情報があります。

于 2012-06-18T15:00:51.917 に答える
10

最後に、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; }
    }
}
于 2012-06-19T08:32:58.177 に答える
2

次のコードの方が優れていると思います。

string version = ((EnvDTE.DTE) ServiceProvider.GlobalProvider.GetService(typeof(EnvDTE.DTE).GUID)).Version;
于 2013-02-17T05:00:36.327 に答える