“未处理 InvalidCastException" [英] "InvalidCastException was unhandled"

查看:59
本文介绍了“未处理 InvalidCastException"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 :)

这一行是错误

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

这是错误

无法将类型为System .__ ComObject"的 COM 对象转换为接口输入Shell32.Shell".操作中出现错误,因为QueryInterface 调用具有 IID 的接口的 COM 组件{} 286E6F1B-7113-4355-9562-96B7E9D64C54"生成了以下内容错误:不支持此类接口(来自 HRESULT 的异常:0x80004002(E_NOINTERFACE) ).

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();
      }
   }
}

推荐答案

显然这是 Windows 8 中的一项功能,它会导致您的原始代码不起作用.我在这里找到了答案:

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

如何在 C# 应用程序中使用 Shell32?

我更新了下面的代码.已在 Win 8 pro 上测试并运行.

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     });
}

  }
}

这篇关于“未处理 InvalidCastException"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆