2026年5月20日水曜日

VB OS のバージョンを取得 ( Windows11 対応 )

お約束の Code を。
そう、
.Netframework 4.8.1 で Windows 11 の OS 絡みの情報を。
ばらばらに格納された値達を掻き集めます。


VS OS のバージョンを取得 ( Windows11 対応 ) 序  に続きます。


さて、お約束の Code を。


前回の最後に触れた、

コード中に残したメモから。
// WMI ManagementObject .Properties["Caption"].Value > Microsoft Windows 11 Pro
// reg HKEY_LOCAL_MACHINE¥SOFTWARE¥Microsoft¥Windows NT¥CurrentVersion DisplayVersion > 23H2
// cmd.exe ver > Microsoft Windows [Version 10.0.22631.4317]
ほんと、ばらばらに散らばって格納されている ... 。

を、具体化したもの。
3項目の順番は 入れ替わっていますので ご留意を。


では。


Class Library (.dll) にする心算のものから 抜粋 します。
Windows 11 23H2 の頃に書いたものがベースですが、
OS が 25H2 に変わっても、今の処、障害は無さそうです。

開発言語は C# 。
使うのは、 .Netframework 4.8.1 。
Visual Studio 2022 を導入しましたので、其れで書き出しました。

OS: Windows 10 は、既に自分の環境では持って無い(更新済み)ので、
検証していません。  エラー処理も含めてです。
Only for Windows 11 とお考え下さい。

得られる文字列は、将来の変化に対応する為に、余り加工せずに、取得しています。
勿論、要求される(欲しい)形に 成形加工 するものも 別途 用意しますが、
此処では省略します。

また、最終的には、.dll にしますので、
元の Source Code が VB.Net であろうが、C# であろうが、
また、Form であろうが、WPF であろうが、
使い廻しが効きます。
単に文字列を返す形にしたのは其れが目的でもあるから、です。

因みに、
Static を付けて、静的 にした方が良いのかも。



今現在の .dll Test Case 。 Instance and Static Classes 。




    public string GetWin11OsName()
    {
        // Need to Refer/Using System.Management

        string os = "";
        using (var objOS = new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem"))
        {
            foreach (ManagementObject objMgmt in objOS.Get())
            {
                //Console.WriteLine("{0}: {1}", objMgmt.Properties["Caption"].Name, objMgmt.Properties["Caption"].Value);
                os = objMgmt.Properties["Caption"].Value.ToString()
            }
        }
        return os;
    }

// Output : Microsoft Windows 11 Pro



    public string GetWin11OsBuild()
    {
        System.Diagnostics.Process p = new System.Diagnostics.Process();

        p.StartInfo.FileName = "cmd.exe";
        p.StartInfo.UseShellExecute = false;
        p.StartInfo.RedirectStandardOutput = true;
        p.StartInfo.RedirectStandardInput = false;
        p.StartInfo.CreateNoWindow = true;
        p.StartInfo.Arguments = "/c  ver";
        p.Start();
        string results = p.StandardOutput.ReadToEnd();
        p.WaitForExit();
        p.Close();

        int Idxstart = results.IndexOf("[");
        int Idxend = results.IndexOf("]");
        results = results.Substring(Idxstart + 1, Idxend - Idxstart - 1);

        return results;
    }

// Output : Version 10.0.26200.8457


    public string GetWin11DispVer()
    {
        string key = "Software¥¥Microsoft¥¥Windows NT¥¥CurrentVersion";
        string name = "DisplayVersion";
        Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(key);
        string regVer = (string)regKey.GetValue(name);
        regKey.Close();

        return regVer;
    }

// Output : 25H2



此の手の内容の 一抹の不安 は、
何時迄、正常に稼働するか? でしょう。

相手(OS)あっての事ですから、
Windows の 更新の影響を 諸に! 被りますから。

願わくは、Microsoft さまの基本方針の転換にぶつから無い事 ... 。

出来るなら、此処暫くは ご安泰 で願いたいもの ... 。
果たして、 如何相成ります事やら。



0 件のコメント:

コメントを投稿