【问题标题】:"InvalidCastException was unhandled"“InvalidCastException 未处理”
【发布时间】:2025-12-07 06:20:04
【问题描述】:

我正在尝试做一个应用程序,它可以告诉我所有视频的总时间。它不实用,它只是提高我的编码能力的一种做法:)

在这一行是错误

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

这就是错误

无法将“System .__ ComObject”类型的 COM 对象转换为接口 键入“Shell32.Shell”。操作出现错误,因为 对具有 IID 的接口的 COM 组件上的 QueryInterface 调用 '{} 286E6F1B-7113-4355-9562-96B7E9D64C54' 生成以下内容 错误:不支持此类接口(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();
      }
   }
}

【问题讨论】:

  • 嗯,我猜,不是所有人都能理解你提到的错误。
  • 大多数时候,消息前面都有错误代码。这将有很大帮助,因为它与语言无关。
  • Visual Studio 在此处标记错误 Shell32.Shell shell = new Shell32.Shell();
  • Debugger:Exception Intercepted: Main, Program.cs line 16 一个异常被截获并且调用堆栈展开到发生异常的用户代码调用之前的点。在调试器选项中选择了“Unwind the call stack on unhandled exceptions”。时间:19/11/2014 18:39:17 线程:主线程[27404]
  • Shell 接口有线程要求,这是控制台模式应用程序无法提供的。 GUI 应用程序可以。您可能会通过将 [STAThread] 放在 Main() 方法之上来摆脱它,如果您的程序死锁,您知道您没有这样做。

标签: c# visual-studio-2012


【解决方案1】:

显然,这是 Windows 8 中的一项功能,它会导致您的原始代码无法运行。 我在这里找到了答案:

How to use Shell32 within a C# application?

我在下面更新了你的代码。在 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     });
}

  }
}

【讨论】:

  • 确保您也遵循上面的 rodrigogq 的回答。
【解决方案2】:

确保您在 Windows/system32 中将 Add Reference 转换为 Shell32.dll

即使您正在构建 x86,引用也必须指向 windows/system32 文件夹。

【讨论】: