Windows shell interface reference using .net 4.0

I use the following code to reference shell dll

Type t = Type.GetTypeFromProgID("Shell.Application"); Shell s = (Shell)Activator.CreateInstance(t); Console.WriteLine("success"); Console.ReadLine(); 

It works fine on my Windows 7 development machine. But when I try to run exe on a Win 2003 server, I get this exception

 Unable to cast COM object of type 'System.__ComObject' to interface type 'Shell3 2.Shell'. This operation failed because the QueryInterface call on the COM compo nent for the interface with IID '{866738B9-6CF2-4DE8-8767-F794EBE74F4E}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)). 

I helped C # a bit : link to the windows shell interface , but no luck.

I reference the shell using the help of Microsoft Shell Controls and Automation, which is interop.Shell32 dll

If someone can be guided, it will be really helpful.

+8
c # windows-shell
source share
3 answers

Ok, this is how I went through the problem helping someone

This is what my new code looks like

 Type t = Type.GetTypeFromProgID("Shell.Application"); dynamic shell = Activator.CreateInstance(t); //This is browse through all the items in the folder var objFolder = shell.NameSpace(@"\\fileshares\Files\test"); foreach (var item in objFolder.Items()) { //This is to get the file comments for each files in the folderitem string file_version = objFolder.GetDetailsOf(item, 14).ToString(); Console.WriteLine(file_version); } 

This script is to combine help from http://nerdynotes.blogspot.com/2008/06/vbnet-shell32-code-compiled-on-vista.html

and

http://foro.h-sec.org/net/problemas-en-net/

The second link is in Spanish, I used google translate to create it in English

Thanks to everyone who answered this question.

+15
source share
+1
source share

Instead

 Type t = Type.GetTypeFromProgID("Shell.Application"); dynamic shell = Activator.CreateInstance(t); 

I used

 var shell = (IShellDispatch4) new Shell(); 

shell.Namespace then works as expected.

It turns out that this reference for the shell object defaults to IShellDispatch5, which cannot be used in XP or 2003.

+1
source share

All Articles