Use C# to interact with Windows Update

前端 未结 4 1064
南方客
南方客 2020-12-03 03:49

Is there any API for writing a C# program that could interface with Windows update, and use it to selectively install certain updates?

I\'m thinking somewhere along

4条回答
  •  死守一世寂寞
    2020-12-03 03:53

    Add a Reference to WUApiLib to your C# project.

    using WUApiLib;
    protected override void OnLoad(EventArgs e){
        base.OnLoad(e);
        UpdateSession uSession = new UpdateSession();
        IUpdateSearcher uSearcher = uSession.CreateUpdateSearcher();
        uSearcher.Online = false;
        try {
            ISearchResult sResult = uSearcher.Search("IsInstalled=1 And IsHidden=0");
            textBox1.Text = "Found " + sResult.Updates.Count + " updates" + Environment.NewLine;
            foreach (IUpdate update in sResult.Updates) {
                    textBox1.AppendText(update.Title + Environment.NewLine);
            }
        }
        catch (Exception ex) {
            Console.WriteLine("Something went wrong: " + ex.Message);
        }
    }
    

    Given you have a form with a TextBox this will give you a list of the currently installed updates. See http://msdn.microsoft.com/en-us/library/aa387102(VS.85).aspx for more documentation.

    This will, however, not allow you to find KB hotfixes which are not distributed via Windows Update.

提交回复
热议问题