问题描述
我正在编写一个应检测,下载并安装Windows Update的应用程序。但是,如果没有提升的特权,我无法让非管理员安装更新。但是,我已将本地策略配置为启用(计算机配置>管理模板> Windows组件> Windows更新>允许非管理员接收更新通知),根据帮助,该策略将允许非管理员安装。但是,除非将WUApiLib IUpdateInstaller :: install和UpdateDownloader :: download方法提升,否则它们将不起作用。
我们如何完成与本机应用程序类似的工作?
想要的人的相关代码:
public void InstallUpdates(UpdateCollection updatesCollection)
{
UpdateSession uSession = new UpdateSession();
IUpdateSearcher uSearcher = uSession.CreateUpdateSearcher();
UpdateCollection updatesToInstall = new UpdateCollection();
UpdateDownloader downloader = uSession.CreateUpdateDownloader();
IUpdateInstaller installer = uSession.CreateUpdateInstaller();
foreach (IUpdate update in updatesCollection)
{
if (update.IsDownloaded == false)
{
ProgramLog.LogMessagetoFile("Downloading " + update.Title + "...");
do
{
downloader.Updates.Add(update);
downloader.Download();
}
while (update.IsDownloaded == false);
ProgramLog.LogMessagetoFile(update.Title + " downloaded");
ProgramLog.LogMessagetoFile("Adding " + update.Title + " to list of updates for installation");
updatesToInstall.Add(update);
}
else
{
ProgramLog.LogMessagetoFile(update.Title + " is already downloaded");
ProgramLog.LogMessagetoFile("Adding " + update.Title + " to list of updates for installation");
updatesToInstall.Add(update);
}
}
installer.Updates = updatesToInstall;
ProgramLog.LogMessagetoFile("Beginning installation of " + installer.Updates.Count.ToString() + " updates");
installer.Install();
IInstallationResult installationRes = installer.Install();
for (int i = 0; i < updatesToInstall.Count; i++)
{
ProgramLog.LogMessagetoFile("Installing " + updatesToInstall[i].Title + "...");
if (installationRes.GetUpdateResult(i).HResult == 0)
{
ProgramLog.LogMessagetoFile(updatesToInstall[i].Title + " installed successfully!");
}
else
{
ProgramLog.LogMessagetoFile("Failed to install " + updatesToInstall[i].Title);
}
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)