如何可靠地检查是否使用C#启用了Windows Update?

时间:2012-10-31 20:30:53

标签: c# com windows-update

我尝试检查是否启用了Windows Update。我在Windows 7 x64 Ultimate上添加了对c:\ windows \ system32 \ wuapi.dll的引用并编写了此代码

using WUApiLib;
public Boolean IsWindowsUpdateEnabled()
{
    var updates = new AutomaticUpdatesClass();
    return updates.ServiceEnabled;
}

代码无法构建。我收到以下错误:

  

错误1类型'WUApiLib.AutomaticUpdatesClass'没有构造函数   定义
  错误2互操作类型'WUApiLib.AutomaticUpdatesClass'不能   嵌入式。请改用适用的界面   错误3'WUApiLib.AutomaticUpdatesClass'不包含定义   对于'ServiceEnabled'并且没有扩展方法'ServiceEnabled'   接受'WUApiLib.AutomaticUpdatesClass'类型的第一个参数   可以找到(你错过了使用指令或程序集   引用?)

2 个答案:

答案 0 :(得分:3)

也许您可以查询注册表以查看?

public string Read(string KeyName)
{
    // Opening the registry key
    RegistryKey rk = baseRegistryKey ;
    // Open a subKey as read-only
    RegistryKey sk1 = rk.OpenSubKey(subKey);
    // If the RegistrySubKey doesn't exist -> (null)
    if ( sk1 == null )
    {
        return null;
    }
    else
    {
        try 
        {
            // If the RegistryKey exists I get its value
            // or null is returned.
            return (string)sk1.GetValue(KeyName.ToUpper());
        }
        catch (Exception e)
        {
            // AAAAAAAAAAARGH, an error!
            ShowErrorMessage(e, "Reading registry " + KeyName.ToUpper());
            return null;
        }
    }
}

source

要查看的位置:

http://www.windowsnetworking.com/articles_tutorials/Registry-Keys-Tweaking-Windows-Update-Part1.html

  

HKEY_LOCAL_MACHINE \ SOFTWARE \政策\微软\的Windows \ WindowsUpdate的\ AU   ...

     

这些键中的第一个是AUOptions键。这个DWORD值可以   分配值为2,3,4或5.值为2表示   代理应在下载更新之前通知用户。一个值   3表示将自动下载更新和   将通知用户安装。值为4表示   应根据自动下载和安装更新   时间表。要使此选项起作用,可以使用ScheduledInstallDay和   还必须设置ScheduledInstallTime键。我会谈谈更多   那些钥匙以后。最后,值为5表示自动   需要更新,但最终用户可以配置。

如果设置是由组策略完成的,可能会有所不同。

此处有更多信息:http://support.microsoft.com/kb/328010

答案 1 :(得分:3)

在Visual Studio项目引用列表中,找到WUApiLib引用并将其“嵌入互操作类型”更改为“False”。

相关问题