2009-05-28 128 views
14

是否有任何用于编写C#程序的API,该程序可以与Windows更新进行交互,并使用它来选择性地安装某些更新?使用C#与Windows Update进行交互

我正在考虑在存储已批准更新的中央存储库列表中的某处。然后,客户端应用程序(必须安装一次)将与Windows Update进行接口,以确定可用的更新,然后安装已批准列表中的更新。这样更新仍然从客户端角度自动应用,但我可以选择正在应用的更新。

这不是我在公司的角色,我真的只是想知道是否有一个API的Windows更新以及如何使用它。

+0

你一定要找Windows Update代理API:http://msdn.microsoft.com/en-us/library/aa387099.aspx – 2009-05-28 17:25:12

回答

19

添加引用WUApiLib到C#项目。

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); 
    } 
} 

鉴于你有一个文本框的表单这会给你当前已安装的更新列表。有关更多文档,请参阅http://msdn.microsoft.com/en-us/library/aa387102(VS.85).aspx

但是,这将不允许您查找未通过Windows Update分发的KB修补程序。

4

最简单的做法是使用WSUS。它是免费的,基本上可以让你设置你自己的本地Windows更新服务器,你可以决定哪些更新被“批准”用于你的电脑。 WSUS服务器和客户端都不需要位于域中,但如果它们更容易配置客户端,则WSUS服务器和客户端都不需要位于域中。如果你有不同的机器需要不同的更新批准,那也是支持的。

这不仅可以达到您的既定目标,还可以通过从WSUS服务器仅下载一次更新来节省您的整体网络带宽。

1

P-L的权利。我首先尝试了Christoph Grimmer-Die方法,在某些情况下,它不起作用。我想这是由于.net或OS体系结构(32或64位)的不同版本。 然后,以确保我的程序总是得到我的每一个计算机领域的Windows更新的等待列表中,我做了以下内容:

它会在控制台打印带有UpdateInstallationStates所有Windows更新。下载

using System; 
using Microsoft.UpdateServices.Administration; 
using SimpleImpersonation; 

namespace MAJSRS_CalendarChecker 
{ 
    class WSUS 
    { 
     public WSUS() 
     { 
      // I use impersonation to use other logon than mine. Remove the following "using" if not needed 
      using (Impersonation.LogonUser("mydomain.local", "admin_account_wsus", "Password", LogonType.Batch)) 
      { 
       ComputerTargetScope scope = new ComputerTargetScope(); 
       IUpdateServer server = AdminProxy.GetUpdateServer("wsus_server.mydomain.local", false, 80); 
       ComputerTargetCollection targets = server.GetComputerTargets(scope); 
       // Search 
       targets = server.SearchComputerTargets("any_server_name_or_ip"); 

       // To get only on server FindTarget method 
       IComputerTarget target = FindTarget(targets, "any_server_name_or_ip"); 
       Console.WriteLine(target.FullDomainName); 
       IUpdateSummary summary = target.GetUpdateInstallationSummary(); 
       UpdateScope _updateScope = new UpdateScope(); 
       // See in UpdateInstallationStates all other properties criteria 
       _updateScope.IncludedInstallationStates = UpdateInstallationStates.Downloaded; 
       UpdateInstallationInfoCollection updatesInfo = target.GetUpdateInstallationInfoPerUpdate(_updateScope); 

       int updateCount = updatesInfo.Count; 

       foreach (IUpdateInstallationInfo updateInfo in updatesInfo) 
       { 
        Console.WriteLine(updateInfo.GetUpdate().Title); 
       } 
      } 
     } 
     public IComputerTarget FindTarget(ComputerTargetCollection coll, string computername) 
     { 
      foreach (IComputerTarget target in coll) 
      { 
       if (target.FullDomainName.Contains(computername.ToLower())) 
        return target; 
      } 
      return null; 
     } 
    } 
} 
1

我写了一些dummycode这个(一个简单的控制台应用程序,将安装基于命令行参数,我们希望所有的更新,它可以在这里找到,并包含执行更新所需的一切)

https://bitbucket.org/LilleCarl/c-windowsupdate

,如果你想你需要做自己:

Add reference to C:\Windows\System32\wuapi.dll 
using WUApiLib; 

有用的界面:

UpdateSession 
UpdateCollection 
UpdateDownloader 
IUpdateInstaller 
IInstallationResult 
相关问题