Reputation: 792
I am looking for an interface that can be used to trigger a C# script whenever a new update is installed on Windows 10.
I thought of polling the current Windows version every day and checking if it's a new one, but it doesn't sound very efficient.
I am thinking of running this script daily; so the suggested method won't work as expected. Is there a way to check last time an update was installed?
Upvotes: 1
Views: 201
Reputation: 1689
Windows Update Agent has a COM object that can be used to query Windows Update.
Here is an example:-
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();
}
}
}
(you'll need a reference to the COM library "WUAPI 2.0 Type Library" in the project)
The above example could be used to check if the number of installed updates has changed since it was last run.
Check the referenced documentation for the right query to use to suit you; for example, some updates may be hidden.
Upvotes: 2