VSPackageで実行されているVisualStudioのバージョンを確認/検出するにはどうすればよいですか?
コンピューターに複数のバージョンがインストールされている可能性があるため、レジストリから取得できません。そのため、レジストリを取得できるAPIがあると思います。
C#を使用してマネージドVisual Studioパッケージから取得する方法を知っている人はいますか?
VSPackageで実行されているVisualStudioのバージョンを確認/検出するにはどうすればよいですか?
コンピューターに複数のバージョンがインストールされている可能性があるため、レジストリから取得できません。そのため、レジストリを取得できるAPIがあると思います。
C#を使用してマネージドVisual Studioパッケージから取得する方法を知っている人はいますか?
オートメーション 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 にも役立つ情報があります。
最後に、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; }
}
}
次のコードの方が優れていると思います。
string version = ((EnvDTE.DTE) ServiceProvider.GlobalProvider.GetService(typeof(EnvDTE.DTE).GUID)).Version;