我正在寻找一个可用于在 Windows 10 上安装新更新时触发 C# 脚本的界面。
我想每天轮询当前的 Windows 版本并检查它是否是新版本,但听起来效率不高。
编辑
我正在考虑每天运行这个脚本;所以建议的方法不会按预期工作。有没有办法检查上次安装更新的时间?
最佳答案
Windows Update Agent有一个可用于查询 Windows 更新的 COM 对象。
这是一个例子:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WUApiLib;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
var session = new UpdateSession();
var searcher = session.CreateUpdateSearcher();
searcher.Online = false;
var result = searcher.Search("IsInstalled=1");
Console.WriteLine($"Found {result.Updates.Count} installed updates");
Console.Read();
}
}
}
(您需要在项目中引用 COM 库“WUAPI 2.0 类型库”)
以上示例可用于检查自上次运行以来安装的更新数量是否发生了变化。
查看引用文档以找到适合您的正确查询;例如,某些更新可能会被隐藏。
关于c# - Windows 更新 Hook ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57320195/