Detecting Microsoft patch updates

Pasam Pradeep Sabarinadh 0 Reputation points
2025-03-07T04:20:44.4133333+00:00

Hi All,

I am looking for detecting Microsoft patch updates programmatically using C#.

The idea is to know the patch update time on the windows machines.

Any help is much appreciated.

Regards,

Pradeep Pasam

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,333 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Jiale Xue - MSFT 49,126 Reputation points Microsoft External Staff
    2025-03-07T05:59:54.47+00:00

    Hi @Pasam Pradeep Sabarinadh , Welcome to Microsoft Q&A,

    Windows Update API (Windows Update Agent API) provides more detailed patch information (such as KB number, installation status, time, etc.)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace xxx
    {
        class Program
        {
            static void Main(string[] args)
            {
                Type updateSessionType = Type.GetTypeFromProgID("Microsoft.Update.Session");
            dynamic updateSession = Activator.CreateInstance(updateSessionType);
            dynamic updateSearcher = updateSession.CreateUpdateSearcher();
    
            try
            {
                dynamic searchResult = updateSearcher.Search("IsInstalled=1");
                foreach (var update in searchResult.Updates)
                {
                    Console.WriteLine($"Title: {update.Title}");
                    Console.WriteLine($"KB Article: {update.KBArticleIDs[0]}");
                    Console.WriteLine($"Installed Date: {update.LastDeploymentChangeTime}");
                    Console.WriteLine(new string('-', 50));
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error retrieving Windows updates: " + ex.Message);
            }
            }
        }
    }
    
    

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.