2011-07-19 161 views
3

我在這裏寫了一個使用WUA COM接口(IUpdateSearcher,IUpdate等)的應用程序。我使用此應用程序來掃描可用更新,下載更新並安裝它們。一切正常,直到我下載並安裝一些更新有一些用戶界面更新對話框。Windows更新應用程序

當我使用IUpdateSearcher.Search()我得到這個更新,我可以成功地下載它(使用IUpdateDownloader.Download()),但是當我安裝使用IUpdateInstaller2.Install()此更新我無法擺脫的用戶界面。

我的問題是 - 我怎樣才能使這個沉默的安裝?

+0

爲了我的好奇心,請問您爲什麼要編寫這樣的應用程序? – Johnny5

+0

它是大型系統的一部分,控制許多pc ..(其中一個選項是控制eche PC的Windows更新...) – confi

+1

找到解決方案使用IUpdateInstaller2 insten IUpdateInstaller,有「forcesielte」屬性... – confi

回答

3
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using WUApiLib; 
namespace MSHWindowsUpdateAgent 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      UpdatesAvailable(); 
      EnableUpdateServices();//enables everything windows need in order to make an update 
      InstallUpdates(DownloadUpdates()); 
      Console.Read(); 
     } 
     //this is my first try.. I can see the need for abstract classes here... 
     //but at least it gives most people a good starting point. 
     public static void InstalledUpdates() 
     { 
      UpdateSession UpdateSession = new UpdateSession(); 
      IUpdateSearcher UpdateSearchResult = UpdateSession.CreateUpdateSearcher(); 
      UpdateSearchResult.Online = true;//checks for updates online 
      ISearchResult SearchResults = UpdateSearchResult.Search("IsInstalled=1 AND IsHidden=0"); 
      //for the above search criteria refer to 
      //http://msdn.microsoft.com/en-us/library/windows/desktop/aa386526(v=VS.85).aspx 
      //Check the remakrs section 

      foreach (IUpdate x in SearchResults.Updates) 
      { 
       Console.WriteLine(x.Title); 
      } 
     } 
     public static void UpdatesAvailable() 
     { 
      UpdateSession UpdateSession = new UpdateSession(); 
      IUpdateSearcher UpdateSearchResult = UpdateSession.CreateUpdateSearcher(); 
      UpdateSearchResult.Online = true;//checks for updates online 
      ISearchResult SearchResults = UpdateSearchResult.Search("IsInstalled=0 AND IsPresent=0"); 
      //for the above search criteria refer to 
      //http://msdn.microsoft.com/en-us/library/windows/desktop/aa386526(v=VS.85).aspx 
      //Check the remakrs section 

      foreach (IUpdate x in SearchResults.Updates) 
      { 
       Console.WriteLine(x.Title); 
      } 
     } 
     public static UpdateCollection DownloadUpdates() 
     { 
      UpdateSession UpdateSession = new UpdateSession(); 
      IUpdateSearcher SearchUpdates = UpdateSession.CreateUpdateSearcher(); 
      ISearchResult UpdateSearchResult = SearchUpdates.Search("IsInstalled=0 and IsPresent=0"); 
      UpdateCollection UpdateCollection = new UpdateCollection(); 
      //Accept Eula code for each update 
      for (int i = 0; i < UpdateSearchResult.Updates.Count; i++) 
      { 
       IUpdate Updates = UpdateSearchResult.Updates[i]; 
       if (Updates.EulaAccepted == false) 
       { 
        Updates.AcceptEula(); 
       } 
       UpdateCollection.Add(Updates); 
      } 
      //Accept Eula ends here 
      //if it is zero i am not sure if it will trow an exception -- I havent tested it. 

       UpdateCollection DownloadCollection = new UpdateCollection(); 
       UpdateDownloader Downloader = UpdateSession.CreateUpdateDownloader(); 

       for (int i = 0; i < UpdateCollection.Count; i++) 
       { 
        DownloadCollection.Add(UpdateCollection[i]); 
       } 

       Downloader.Updates = DownloadCollection; 
       Console.WriteLine("Downloading Updates"); 
       IDownloadResult DownloadResult = Downloader.Download(); 
       UpdateCollection InstallCollection = new UpdateCollection(); 
       for (int i = 0; i < UpdateCollection.Count; i++) 
       { 
        if (DownloadCollection[i].IsDownloaded) 
        { 
         InstallCollection.Add(DownloadCollection[i]); 
        } 
       } 
       return InstallCollection; 
     } 
     public static void InstallUpdates(UpdateCollection DownloadedUpdates) 
     { 
      UpdateSession UpdateSession = new UpdateSession(); 
      UpdateInstaller InstallAgent = UpdateSession.CreateUpdateInstaller() as UpdateInstaller; 
      InstallAgent.Updates = DownloadedUpdates; 

      //Starts a synchronous installation of the updates. 
      // http://msdn.microsoft.com/en-us/library/windows/desktop/aa386491(v=VS.85).aspx#methods 
      IInstallationResult InstallResult = InstallAgent.Install(); 

     } 
     public static void EnableUpdateServices() 
     { 
      IAutomaticUpdates updates = new AutomaticUpdates(); 
      if (!updates.ServiceEnabled) 
      { 
       Console.WriteLine("Not all updates services where enabled. Enabling Now" + updates.ServiceEnabled); 
       updates.EnableService(); 
       Console.WriteLine("Service enable success"); 
      } 


     } 

    } 
} 
0

jvelez有一個很好的答案,但它不直接回答實際問題:「我怎麼能使這個沉默安裝?」

CONFI - 我建議首先最簡單的方法:

static class Program 
{ 
/// <summary> 
/// The main entry point for the application. 
/// </summary> 
[STAThread] 
static void Main() 
{ 
Application.SetCompatibleTextRenderingDefault(false); 
Application.EnableVisualStyles(); 

Thread thread = new Thread(() => 
{ 
Form1 form1 = new Form1(); 
form1.Visible = false; 
form1.ShowInTaskbar = false; 
Application.Run(form1); 
}); 

thread.SetApartmentState(ApartmentState.STA); 
thread.Start(); 
} 
} 

確保所有的方法對在後臺通過簡單地隱藏你的UI的工作,這將讓你做這無聲的手段。

@Justin Choponis - 我有需要做同樣的confi。 WSUS並非爲我的許多客戶削減它。它的實用和龐大。如果你有一個網絡需要照顧,並且一直都在現場,那麼WSUS會非常有用,除此之外,我覺得它非常令人不滿意。