如何知道Windows更新是否启用C# [英] How to know if windows update is enable C#
本文介绍了如何知道Windows更新是否启用C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何知道是否启用了WIndows更新C#?
我只需要一个C#代码,告诉我是否启用了Windows Update。我尝试过这个库WUApiLib,但我不知道怎么用它来确切地知道我想要的。
这样的东西:
if (xxxxxxxxxxx)
{
MessageBox.Show( Windows Update正在运行);
}
else
{
MessageBox.Show( Windows Update无效);
}
谢谢。
什么我试过了:
使用 WUApiLib;
受保护 覆盖 void OnLoad (EventArgs e){
base .OnLoad(e);
UpdateSession uSession = new UpdateSession();
IUpdateSearcher uSearcher = uSession.CreateUpdateSearcher();
uSearcher.Online = false ;
尝试 {
ISearchResult sResult = uSearcher.Search( IsInstalled = 1并且IsHidden = 0);
textBox1.Text = 找到 + sResult.Updates.Count + updates + Environment.NewLine;
foreach (IUpdate update in sResult.Updates){
textBox1.AppendText( update.Title + Environment.NewLine);
}
}
catch (Exception ex){
Console.WriteLine( 出了点问题: + ex.Message);
}
}
解决方案
首先添加对WUApiLib的引用C:\windows \ system32 \\ \\ wuapi.dll
见下面的片段
WUApiLib.AutomaticUpdatesClass auc = new WUApiLib.AutomaticUpdatesClass();
bool active = auc.ServiceEnabled;
MSDN说:ServiceEnabled属性表示自动更新所需的所有组件是否都可用。
这样的东西:
使用C#枚举和控制Windows服务 [ ^ ]
您是否考虑枚举计算机上运行的服务并查找Windows更新服务 那么 BITS(后台智能转移)并找到 out 他们当前的状态?
How to know if WIndows Update is enabled C#?
I just need a Code in C# that show me if Windows Update is enabled or not. I have tried with this library WUApiLib but I'm not sure how to use it to know exactly I want.
Something like this:
if(xxxxxxxxxxx)
{
MessageBox.Show("Windows Update is working");
}
else
{
MessageBox.Show("Windows Update is not working");
}
Thanks.
What I have tried:
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);
}
}
解决方案
First add a reference to WUApiLib "C:\windows\system32\Wuapi.dll"
see below snippet
WUApiLib.AutomaticUpdatesClass auc = new WUApiLib.AutomaticUpdatesClass(); bool active = auc.ServiceEnabled;
MSDN says :"The ServiceEnabled property indicates whether all the components that Automatic Updates requires are available."
Something along the lines as this:
Enumerating and Controlling Windows Services with C#[^]
Have you considered enumerating the services running on the machine and finding the Windows Update service as well as BITS (background intelligent transfer) and finding out their current state?
这篇关于如何知道Windows更新是否启用C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文