Windows Update Agent API Problem. Cant get Installed Updates.

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I have problem with Windows Update API.
I have 3 updates installed on Win 8 Platform.
var updatesNeedTobBeInstalled = new List<String>() { "KB2764870", "KB2761094", "KB2756872" };
I use this code to print all updates installed on this platform. I get 0 updates. Why? public class WindowsUpdate
{
UpdateCollection InstalledUpdates;
UpdateCollection AvailableUpdates;
UpdateSession Session;
IUpdateSearcher Searcher;
UpdateInstaller Installer;
UpdateDownloader Downloader;

public void EnableUpdateServices()
{
var updateManager = new AutomaticUpdates();
if (!updateManager.ServiceEnabled) updateManager.EnableService();
}

public UpdateCollection GetInstalledUpdates()
{
Searcher.Online = false;
var Result = Searcher.Search("IsInstalled=1 AND IsHidden=0");
InstalledUpdates = Result.Updates;
return Result.Updates;
}

public UpdateCollection GetAvailableUpdates()
{
Searcher.Online = true;
var Result = Searcher.Search("IsInstalled=0 AND IsPresent=0");
AvailableUpdates = Result.Updates;
return Result.Updates;
}

public void AcceptEULA(UpdateCollection updateCollection)
{
for (var i = 0; i < updateCollection.Count; i++)
updateCollection.AcceptEula();
}

public UpdateCollection DownloadUpdates(UpdateCollection toDownloadUpdateCollection)
{
Downloader.Updates = toDownloadUpdateCollection;
Downloader.Download();
var toInstallUpdateCollection = new UpdateCollection();
if (toDownloadUpdateCollection.Count > 0)
for (var i = 0; i < toDownloadUpdateCollection.Count; i++)
if (toDownloadUpdateCollection.IsDownloaded)
toInstallUpdateCollection.Add(toInstallUpdateCollection);
return toInstallUpdateCollection;
}

public void InstallUpdates(UpdateCollection toInstallUpdateCollection)
{
Installer.Updates = toInstallUpdateCollection;
if (toInstallUpdateCollection.Count > 0)
{
var result=Installer.Install();
if (result.ResultCode == OperationResultCode.orcSucceeded)
{
//SUCCESS
if (result.RebootRequired == true)
{ }//Reboot
}
else {//LIPA
}
}
}

public void DownloadAndInstallUpdates(UpdateCollection updateCollection)
{
AcceptEULA(updateCollection);
InstallUpdates(DownloadUpdates(updateCollection));
}

public WindowsUpdate()
{
Session = new UpdateSession();
Searcher = Session.CreateUpdateSearcher();
Installer = Session.CreateUpdateInstaller() as UpdateInstaller;
Downloader = Session.CreateUpdateDownloader();
}
}




class Program
{
static void Main(string[] args)
{
var updates = new WindowsUpdate();

var installedUpdates = updates.GetInstalledUpdates();

var up = new List<String>() { "KB2764870", "KB2761094", "KB2756872" };

foreach (IUpdate x in installedUpdates)
Console.WriteLine(x.Title);
}
}

View the full article
 
Back
Top