0

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();
      }
   }
}
John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • Well I guess, not all people are able to understand the error you mentioned. – shrekDeep Nov 19 '14 at 17:32
  • Most of the time, there is an error code in front of the message. That would help enormously, since it is language independent. – DasKrümelmonster Nov 19 '14 at 17:34
  • Visual studio flags the error here Shell32.Shell shell = new Shell32.Shell(); – Manuel_csharp Nov 19 '14 at 17:38
  • Debugger:Exception Intercepted: Main, Program.cs line 16 An exception was intercepted and the call stack unwound to the point before the call from user code where the exception occurred. "Unwind the call stack on unhandled exceptions" is selected in the debugger options. Time: 19/11/2014 18:39:17 Thread:Main Thread[27404] – Manuel_csharp Nov 19 '14 at 17:39
  • 1
    Shell interfaces have threading requirements, the kind that a console mode app cannot provide. A GUI app does. You'll probably get away with it by putting [STAThread] on top of your Main() method, you know you didn't if your program deadlocks. – Hans Passant Nov 19 '14 at 17:42
  • Are you trying to run this code on Windows 8 maybe? – b2zw2a Nov 19 '14 at 17:44
  • Thanks Hans Passant It worked!!! – Manuel_csharp Nov 19 '14 at 17:58

2 Answers2

0

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.

rodrigogq
  • 1,943
  • 1
  • 16
  • 25
0

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

  }
}
Community
  • 1
  • 1
serial8
  • 60
  • 6