C#でWindows Update
ここではC#を使用してWindows Updateを実行する方法を解説します。
C#からWindows Updateを実行するにはWindows Update Agent(WUA) APIを使用します。
WUA APIをWindows Updateのダウンロードおよびインストールを行うことが出来ます。
以下はWindows Updateのダウンロードからインストールまでの例です。
UpdateSession session = new UpdateSession(); // Windows Updateを検索 IUpdateSearcher searcher = session.CreateUpdateSearcher(); ISearchResult result = searcher.Search("IsInstalled=0 and Type='Software' and AutoSelectOnWebSites=1") // Windows Updateをダウンロード IUpdateDownloader downloader = session.CreateUpdateDownloader() downloader.Updates = result.Updates downloader.Download() // Windows Updateをインストール IUpdateInstaller = session.CreateUpdateInstaller() installer.Updates = result.Updates installer.Install()