使用 WUAPI 在 C# 中悄悄安装更新

发布于 2024-11-30 11:20:50 字数 881 浏览 4 评论 0原文

我正在尝试创建一个可以静默处理更新的程序。我正在使用 wuapilib.dll,它附带了许多类(c#)。我对该程序的第一次修订如下(忽略拼写错误问题 - 它在另一台无法访问互联网的计算机上,所以我手动输入):

IUpdateSession mySess = new UpdateSession();
IUpdateSearcher mySear = mySess.CreateUpdateSearcher();
ISearchResult myRes = mySear.Search("Type='Software'");
IUpdateDownloader myDown = mySess.CreateUpdateDownloader();
IUpdateInstaller myInst = mySess.CreateUpdateInstaller();

myDown.Updates = myRes.Updates;
myDown.Download();
myInst.Updates = myRes.Updates;
myInst.Install();

忽略已经下载或安装更新的情况,我省略了上面的逻辑。我的问题是 IUpdateInstaller 不允许您强制进行静默安装 - 许多更新要求用户单击确认框。 IUpdateInstaller2 类确实如此(我从第二篇文章中得到了这个 这里),但是我一生都找不到获取 IUpdateInstaller2 对象的方法。似乎没有任何东西返回一个,并且微软的文档不包含任何示例代码。广泛的谷歌搜索没有返回任何有用的结果。

我想我已经非常接近了 - 功能就在那里,我只是无法完全访问它。

感谢您的帮助。

I'm trying to create a program that will handle updates silently. I am using the wuapilib.dll, which comes with a number of classes (c#). My first revision of the program was as follows (ignore typo problems - its on another computer without internet access so i'm typing it by hand):

IUpdateSession mySess = new UpdateSession();
IUpdateSearcher mySear = mySess.CreateUpdateSearcher();
ISearchResult myRes = mySear.Search("Type='Software'");
IUpdateDownloader myDown = mySess.CreateUpdateDownloader();
IUpdateInstaller myInst = mySess.CreateUpdateInstaller();

myDown.Updates = myRes.Updates;
myDown.Download();
myInst.Updates = myRes.Updates;
myInst.Install();

Ignore the case where an update is already downloaded or installed, I'm omitting the logic above. My problem is that IUpdateInstaller doesn't allow you to force a quiet install - a number of updates require that a user click a confirmation box. The IUpdateInstaller2 class does (I got that from the second post down here), but for the life of me I can't find a way to get an IUpdateInstaller2 object. Nothing seems to return one, and Microsoft's documentation doesn't contain any example code. Extensive googling returned nothing of use.

I think I'm really close - the functionality is there, I just can't quite access it.

Thanks for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

酒浓于脸红 2024-12-07 11:20:50

我检查了这一点(或者更确切地说,我认为我做了 - 不太清楚如何让它工作),看起来 CreateUpdateInstaller 只返回一个 IUpdateInstaller,没有其他。

然而,我发现代码(在一个中文网站上,很有趣)直接将 IUpdateInstaller 转换为 IUpdateInstalelr2,这解决了我的问题。

感谢您的帮助

I checked that (or rather, I think i did - wasn't too clear on getting it to work), and it looks like the CreateUpdateInstaller only returns an IUpdateInstaller, nothing else.

However, I found code (on a chinese website, interestingly enough) that just directly cast the IUpdateInstaller to an IUpdateInstalelr2, which has solved my problems.

Thanks for the help

喜你已久 2024-12-07 11:20:50

我在另一个问题中发布了我的应用程序,用于搜索、下载然后安装 Windows 更新。

请参阅:C# 和 WUAPI:BeginDownload 函数

您可以轻松更改the:

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

然后

Application.Close();

在事件完成后进行处理。例如,如果未找到更新,则关闭应用程序。我使用接口的异步属性,以便它可以执行异步所需的操作。

希望这有帮助。

I have posted in another Question my app to, search, download and then install Windows updates.

See: C# and WUAPI: BeginDownload function

you can easily change the:

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

then handle

Application.Close();

after the events have done their thing. For example, if no Updates have been found, then close the app. I use the async properties of the interfaces so it can do what it needs to async.

Hope this helps.

醉殇 2024-12-07 11:20:50

我也找了好久才找到。
您只需要投射它即可

IUpdateInstaller2 installer = new UpdateInstaller();

。根据 Microsoft 文档,还有版本 3 和版本 4 可用。但这一定是一个错误。版本 3 的功能在 IUpdateInstaller2 中也可用,而版本 4 的功能我从未在某处找到。

I also searched a long time for it.
You just need to cast it

IUpdateInstaller2 installer = new UpdateInstaller();

According to Microsoft documentation there are also version 3 and 4 available. But this must be an error. The functions of version 3 are available also in IUpdateInstaller2 and the functions from version 4 I never found somewhere.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文