从 .NET 检查是否启用了 Windows 更新
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/682227/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check from .NET if Windows Update is enabled
提问by Ovi
Is there any way to check from .NET if windows update is enabled?
如果启用了 Windows 更新,有没有办法从 .NET 检查?
I want to prompt the users every time they log into my app that their computer might be at risk and give them a link to windows update website (or windows update application from control panel).
我想在用户每次登录我的应用程序时提示他们他们的计算机可能存在风险,并为他们提供指向 Windows 更新网站(或来自控制面板的 Windows 更新应用程序)的链接。
Preferably it should work on XP, Vista and Windows 7. Maybe there is a registry key or even better an API?
最好它应该在 XP、Vista 和 Windows 7 上工作。也许有一个注册表项或者更好的 API?
回答by Michael Piendl
First add a reference to WUApiLib "C:\windows\system32\Wuapi.dll"
首先添加对 WUApiLib "C:\windows\system32\Wuapi.dll" 的引用
Then you can use this code snippet.
然后你可以使用这个代码片段。
WUApiLib.AutomaticUpdatesClass auc = new WUApiLib.AutomaticUpdatesClass();
bool active = auc.ServiceEnabled;
MSDN: "The ServiceEnabled property indicates whether all the components that Automatic Updates requires are available."
MSDN:“ServiceEnabled 属性指示自动更新所需的所有组件是否都可用。”
The Setting auc.Settings.NotificationLevel contains the information about the current mode. http://msdn.microsoft.com/en-us/library/aa385806(VS.85).aspx
Setting auc.Settings.NotificationLevel 包含有关当前模式的信息。http://msdn.microsoft.com/en-us/library/aa385806(VS.85).aspx
回答by User
Of course it is your choice to do that but getting prompted every few minutes that WindowsUpdate was switched off was by far the worst usability issue in XP.
当然,这样做是您的选择,但每隔几分钟提示 WindowsUpdate 已关闭是 XP 中迄今为止最糟糕的可用性问题。
You don't want to irritate your users. You should love them. And definitely not intrude in their private affairs like checking out if WU is off because honestly it's no business of yours.
您不想激怒您的用户。你应该爱他们。绝对不会干涉他们的私人事务,比如检查 WU 是否关闭,因为老实说这不关你的事。
回答by Reed Copsey
I believe you can do this by using the Windows Update Agent API.
我相信您可以通过使用Windows 更新代理 API来做到这一点。
See IAutomaticUpdatesfor specifics.
有关详细信息,请参阅IAutomaticUpdates。
回答by casperOne
You can check the following registry key.
您可以检查以下注册表项。
HKEY_LOCAL_MACHINE
SOFTWARE
Microsoft
Active Setup
Installed Components
{89820200-ECBD-11cf-8B85-00AA005B4340}
If its IsInstalled value is 1 then Windows Update is installed.
如果其 IsInstalled 值为 1,则安装 Windows 更新。
This was taken from:
这是取自:
I actually REALLY like another answerto this question, but unfortunately, it's only supported on XP SP3, which might not be feasible.
我实际上真的很喜欢这个问题的另一个答案,但不幸的是,它仅在 XP SP3 上受支持,这可能不可行。
回答by Stephen Wrighton
Alternatively you can check to see if the Windows Update service is running using the PROCESS objects.
或者,您可以使用 PROCESS 对象检查 Windows 更新服务是否正在运行。
Something along these lines:
沿着这些路线的东西:
Function CheckIfServiceIsRunning(ByVal serviceName As String) As Boolean
Dim mySC As ServiceProcess.ServiceController
mySC = New ServiceProcess.ServiceController(serviceName)
If mySC.Status = ServiceProcess.ServiceControllerStatus.Stopped Then
' Service isn't running
Return False
ElseIf mySC.Status = ServiceProcess.ServiceControllerStatus.Running Then
' Service already running
Return True
End If
End Function
if memory serves, the applicable service is named "Wuauserv"
如果没记错,适用的服务名为“Wuauserv”
回答by Stuart Kacy
Michael Piendl's answer didn't work for me, but it gave me the information I needed to make it work:
Michael Piendl 的回答对我不起作用,但它为我提供了使其工作所需的信息:
WUApiLib.AutomaticUpdatesClass auc = new WUApiLib.AutomaticUpdatesClass();<br>
string notificationLevel = auc.Settings.NotificationLevel.ToString();<br><br>
The notificationLevel
string will equal different things depending on what option is chosen from the "Automatic Updates" dialog.
notificationLevel
根据从“自动更新”对话框中选择的选项,字符串将等于不同的东西。