[C#] Windows 及び Internet Explorer のバージョンを調べる
■ 概要
C# で Windows 及び Internet Explorer のバージョンを調べる方法を示す。
■ 解説
・C# での Windows のバージョンの取得方法
Windows のバージョンは、System.Environment.OSVersion.Version で取得できる。
例えば、Windows 8 のバージョンは “6.2.9200.0” ですが、メジャー バージョンである最初の 6 だけを得たい場合は、System.Environment.OSVersion.Version.Major で int として取得できる。
ちなみに、マイナー バージョンの 2 の部分の取得には、System.Environment.OSVersion.Version.Minor を使う。
・C# での Internet Explorer のバージョンの取得方法
Internet Explorer のバージョンはレジストリで確認することができる。
レジストリの HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer の Version の値や svcVersion の値で確認できる (Internet Explorer 9 以前は Version、Internet Explorer 10 は svcVersion)。
下のサンプル コードでは、Registry.LocalMachine.OpenSubKey メソッドでキーをオープンし、キーの GetValue メソッドで値を取得している。
ちなみに、Internet Explorer のバージョンは次のサイトで確認することができる。
例えば、Windows 8 の Internet Explorer 10 では、”10.0.9200.16384″ だ。
■ サンプル コード
using Microsoft.Win32; using System; namespace IEVersionChecker { public static class VersionChecker { public static Version WindowsVersion { get { return Environment.OSVersion.Version; } } public static int WindowsMajorVersion { get { return WindowsVersion.Major; } } public static string InternetExplorerVersion // 取得失敗時は null { get { const string internetExplorerKeyName = @"SOFTWARE\Microsoft\Internet Explorer"; // レジストリのこの場所を確認 const string svcVersionValueName = "svcVersion"; // Internet Explorer 10 からはこれの値でチェック const string versionValueName = "Version" ; // Internet Explorer 9 以前はこれの値でチェック try { using (var key = Registry.LocalMachine.OpenSubKey(internetExplorerKeyName)) { return (string)(key.GetValue(svcVersionValueName) ?? key.GetValue(versionValueName)); } } catch (Exception) { return null; } } } public static int InternetExplorerMajorVersion // 取得失敗時は 0 { get { var internetExplorerVersion = InternetExplorerVersion; if (!string.IsNullOrWhiteSpace(internetExplorerVersion)) { var versionTexts = internetExplorerVersion.Split(new Char[] { '.' }); if (versionTexts.Length > 0) { int majorVersion; if (int.TryParse(versionTexts[0], out majorVersion)) return majorVersion; } } return 0; } } } class Program { static void Main() { Console.WriteLine("Windows のバージョンは、{0}。", VersionChecker.WindowsVersion); Console.WriteLine("Windows のメジャー バージョンは、{0}。", VersionChecker.WindowsMajorVersion); Console.WriteLine("Internet Explorer のバージョンは、{0}。", VersionChecker.InternetExplorerVersion); Console.WriteLine("Internet Explorer のメジャー バージョンは、{0}。", VersionChecker.InternetExplorerMajorVersion); } } }
■ 実行方法
- Windows 上の Visual Studio 2012 以降で「コンソール アプリケーション Visual C#」を新規作成し、Program.cs を上のものに書き換える。
- メニューの「デバッグ」-「デバッグなしで開始」を選択するか、Ctrl + F5 キーを押して実行する。
- コマンド プロンプトが起動し、例えば次のように表示される。
Windows のバージョンは、6.2.9200.0。 Windows のメジャー バージョンは、6。 Internet Explorer のバージョンは、10.0.9200.16635。 Internet Explorer のメジャー バージョンは、10。 続行するには何かキーを押してください . . .
ディスカッション
コメント一覧
まだ、コメントがありません