Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#wuapilib-Windows更新(控制台应用程序)失败-无效强制转换异常_C#_Exception_Casting_Installation - Fatal编程技术网

C#wuapilib-Windows更新(控制台应用程序)失败-无效强制转换异常

C#wuapilib-Windows更新(控制台应用程序)失败-无效强制转换异常,c#,exception,casting,installation,C#,Exception,Casting,Installation,环境: Microsoft Visual Studio 2010 SP1 C#--.NET 4.0 问题: 除了安装外,一切正常 经过深思熟虑和研究,我怀疑问题是试图从--“System.\uu ComObject类型的COM对象”转换为接口类型“WUApiLib.UpdateInstaller”。从代码中可以看出,IID和类型已经设置,但参数的值值得怀疑。以下是运行时错误报告: 捕获到System.InvalidCastException。Message=指定的强制转换不可用 有效。Sourc

环境:

Microsoft Visual Studio 2010 SP1

C#--.NET 4.0

问题:
除了安装外,一切正常

经过深思熟虑和研究,我怀疑问题是试图从--“System.\uu ComObject类型的COM对象”转换为接口类型“WUApiLib.UpdateInstaller”。从代码中可以看出,IID和类型已经设置,但参数的值值得怀疑。以下是运行时错误报告:

捕获到System.InvalidCastException。Message=指定的强制转换不可用 有效。Source=程序名StackTrace: 在WUApiLib.IUpdateInstaller.Install()上 在%PATH%\visual studio 2010\Projects\中的程序\u NAME.WindowsUpdates.InstallUpdates(UpdateCollection updatesToInstall)中… 内部异常:

请求:

经过数小时的研究和学习,如果您能帮助解决安装更新的问题,我们将不胜感激。请参阅以下代码片段

程序尝试以下操作:

--已启用Windows update服务(没有问题)

--主机需要更新:(无问题)

--下载更新:(没有问题)

--尝试安装更新。(例外:指定的强制转换无效。)

publicstaticvoidinstallupdates(UpdateCollection updatesToInstall)
{
IUpdateInstaller updateInstaller;
WriteLine(“正在创建UpdateInstaller…”);
//键入itype=Type.GetTypeFromProgID(“Microsoft.Update.Session”,true);
//Type t=Type.GetTypeFromProgID(“Microsoft.Update.Session”,“127.0.0.1”)
键入itype=Type.GetTypeFromProgID(“Microsoft.Update.Installer”,true);
updateInstaller=(updateInstaller)Activator.CreateInstance(itype);
updateInstaller.Updates=updateInstall;
updateInstaller.ClientApplicationID=“{3442D4FE-224D-4CEE-98CF-30E0C4D229E6}”;//不确定IID
IInstallationResult安装结果;
WriteLine(“正在创建UpdateInstallationResult…”);
updateInstaller.IsForced=true;
if(updateInstaller.IsBusy==false)
{
尝试
{
installationResult=updateInstaller.Install();//***此处出现错误强制转换异常***
如果(updatesToInstall.Count>0)
{
对于(int i=0;i
嗯,看来我得自己解决这个问题了。这通常就是它的工作方式。一旦我有了解决方案,我会发布。仅供参考:我终于用一种方法完全重新编写了代码。一旦我这么做了,一切都很顺利。
public static UpdateCollection DownloadUpdates()
{
    // Returns UpdateCollection object. 
}
public static void InstallUpdates(UpdateCollection updatesToInstall)
{

     IUpdateInstaller updateInstaller;
     Console.WriteLine("Creating UpdateInstasller...");

    //Type itype = Type.GetTypeFromProgID("Microsoft.Update.Session", true);
    //Type t = Type.GetTypeFromProgID("Microsoft.Update.Session", "127.0.0.1")
    Type itype = Type.GetTypeFromProgID("Microsoft.Update.Installer", true);

    updateInstaller = (UpdateInstaller)Activator.CreateInstance(itype);
    updateInstaller.Updates = updatesToInstall;
    updateInstaller.ClientApplicationID = "{3442D4FE-224D-4CEE-98CF-30E0C4D229E6}"; //    Unsure of IID
    IInstallationResult installationResult;
    Console.WriteLine("Creating UpdateInstallationResult...");
    updateInstaller.IsForced = true;

    if (updateInstaller.IsBusy == false)
    {
       try
       {
          installationResult = updateInstaller.Install(); // ***Bad Cast exception here.***
          if (updatesToInstall.Count > 0)
          {
              for (int i = 0; i < updatesToInstall.Count; i++)
              {
               if (installationResult.GetUpdateResult(i).HResult == 0)
                         Console.WriteLine("Installed : " + updatesToInstall[i].Title);
                    else
                         Console.WriteLine("Failed : " + updatesToInstall[i].Title);

                    // Is Reboot Required?
                    if (installationResult.RebootRequired == true)
                        Console.WriteLine("Reboot is required for one of more updates.");
               } // end for
          } // end if 
        } // end try
        catch (Exception e)
        {
           Console.WriteLine("Error:: " + e.Message + "\n");
        }

    } // end if
} // end method