2009-05-28 123 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 
相關問題