“InvalidCastException was unhandled”

只谈情不闲聊 提交于 2020-01-07 05:10:30

问题


I'm trying to do an app which tells me the total times of all videos. Its not practical it's only a practice to improve my coding abilities :)

In this line is the error

Shell32.Shell shell = new Shell32.Shell();

And this is the error

Unable to cast COM object of type 'System .__ ComObject' to interface type 'Shell32.Shell'. There was an error in operation because the QueryInterface call on the COM component for the interface with IID '{} 286E6F1B-7113-4355-9562-96B7E9D64C54' generated the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE) ).

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Shell32;

namespace cuentavideosconsola
{
    class Program
    {
    static void Main(string[] args)
    {
        double contartiempo = 0;
        Shell32.Shell shell = new Shell32.Shell();
        Shell32.Folder carpeta;

        carpeta = shell.NameSpace(@"D:\");

        foreach(Shell32.FolderItem2 item in carpeta.Items()){
            Console.WriteLine(carpeta.GetDetailsOf(item,27));
            TimeSpan tiempo = TimeSpan.Parse(carpeta.GetDetailsOf(item,27));
            contartiempo += tiempo.TotalSeconds;
        }
        Console.WriteLine("El total de tiempo de los videos es: " + contartiempo);
        Console.ReadLine();
      }
   }
}

回答1:


Apparently it's a feature in windows 8 which causes your original code not to work. I found the answer here:

How to use Shell32 within a C# application?

I updated you code below. Tested and works on Win 8 pro.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Shell32;



namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
  double contartiempo = 0;
  //Shell32.Shell shell = new Shell32.Shell();
  Shell32.Folder carpeta;

  carpeta = GetShell32Folder(@"D:\");

  foreach (Shell32.FolderItem2 item in carpeta.Items()) {
    Console.WriteLine(carpeta.GetDetailsOf(item, 27));
    TimeSpan tiempo = TimeSpan.Parse(carpeta.GetDetailsOf(item, 27));
    contartiempo += tiempo.TotalSeconds;
  }
  Console.WriteLine("El total de tiempo de los videos es: " + contartiempo);
  Console.ReadLine();
}

private static Shell32.Folder GetShell32Folder(string folderPath)
{
  Type shellAppType = Type.GetTypeFromProgID("Shell.Application");
  Object shell = Activator.CreateInstance(shellAppType);
  return (Shell32.Folder)shellAppType.InvokeMember("NameSpace",
  System.Reflection.BindingFlags.InvokeMethod, null, shell, new object[] { folderPath     });
}

  }
}



回答2:


Make sure you Add Reference to Shell32.dll from your Windows/system32.

Even if you are building a x86, the reference must point to windows/system32 folder.



来源:https://stackoverflow.com/questions/27023191/invalidcastexception-was-unhandled

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!