前几篇文章讲述了如何查看更新软件,这篇讲述如何下载更新及安装
下载前,先要确定是否有更新,然后确定下载哪些更新,当然也可以下载全部更新(这时候类似于XP的盗版更新补丁就能避免下载安装)。
涉及到UpdateSearcherClass,ISearchResult,UpdateDownloaderClass,UpdateInstallerClass类
在下载点用UpdateSearcherClass的实例开始搜索,将搜索结果放到ISearchResult中。
然后实例化一个UpdateDownloaderClass对象,将搜索结果给UpdateDownloaderClass实例对象
调用该实例的Download方法就开始下载了。该方法不是异步方法,所以会直到下载完才执行下面的方法。
安装:
实例化一个UpdateInstallerClass类,将搜索结果给该实例后,然后调用Install方法,就可以安装了。
在调查中,机器环境是vista,当系统的自动更新开启并下载时,用自己写的程序更新完,系统的自动更新程序不会有任何反应。
参考代码:
下载前,先要确定是否有更新,然后确定下载哪些更新,当然也可以下载全部更新(这时候类似于XP的盗版更新补丁就能避免下载安装)。
涉及到UpdateSearcherClass,ISearchResult,UpdateDownloaderClass,UpdateInstallerClass类
在下载点用UpdateSearcherClass的实例开始搜索,将搜索结果放到ISearchResult中。
然后实例化一个UpdateDownloaderClass对象,将搜索结果给UpdateDownloaderClass实例对象
调用该实例的Download方法就开始下载了。该方法不是异步方法,所以会直到下载完才执行下面的方法。
安装:
实例化一个UpdateInstallerClass类,将搜索结果给该实例后,然后调用Install方法,就可以安装了。
在调查中,机器环境是vista,当系统的自动更新开启并下载时,用自己写的程序更新完,系统的自动更新程序不会有任何反应。
参考代码:
1
public void UpdateDownload()
2
{
3
WUApiLib.UpdateSearcherClass upSearch = new WUApiLib.UpdateSearcherClass();
4![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
5
if( upSearch.Online )
6
{
7
WUApiLib.ISearchResult resSearch = upSearch.Search("IsInstalled = 0");
8![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
WUApiLib.UpdateDownloaderClass upDownload = new WUApiLib.UpdateDownloaderClass();
10
WUApiLib.UpdateInstallerClass upInstall = new WUApiLib.UpdateInstallerClass();
11![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
upDownload.Updates = resSearch.Updates;
13![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
14
System.Windows.Forms.MessageBox.Show("Start Download");
15![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
if (upDownload.Updates.Count > 0)
17
{
18
upDownload.Download();
19
}
20![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
System.Windows.Forms.MessageBox.Show("Start Install");
22![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
upInstall.Updates = upDownload.Updates;
24![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
if(upInstall.Updates.Count > 0)
26
{
27
upInstall.Install();
28
}
29![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
30
System.Windows.Forms.MessageBox.Show("Install Ok");
31
}
32
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
24
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
26
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
27
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
28
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
29
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
30
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
31
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
32
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)