【问题标题】:reference a windows shell interface using .net 4.0使用 .net 4.0 引用 Windows shell 接口
【发布时间】:2012-08-22 14:19:23
【问题描述】:

我使用下面的代码来引用一个shell dll

            Type t = Type.GetTypeFromProgID("Shell.Application");

            Shell s = (Shell)Activator.CreateInstance(t);


            Console.WriteLine("success");
            Console.ReadLine();

它在我的 windows 7 开发机器上运行良好。但是当我尝试在 Win 2003 服务器上运行 exe 时,我得到了这个异常

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

我从C#: Referencing a windows shell interface 那里得到了一些帮助,但没有运气。

我正在使用 Microsoft Shell 控件和自动化参考来引用 shell,即 Interop.Shell32 dll

如果有人可以指导,那真的很有帮助。

【问题讨论】:

  • 它很老了,我认为指南没有改变,但谁知道呢。在该机器上运行 regedit.exe 并导航到 HKCR\Shell.Application。验证 CLSID 键值为 {13709620-C279-11CE-A49E-444553540000}
  • 有更简单的解决方案,见stackoverflow.com/a/24967301/625349

标签: c# c#-4.0 windows-shell


【解决方案1】:

好的,这就是我解决问题的方法,以防它帮助某人

这就是我的新代码的样子

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's comments for each files in the folderitem

    string file_version = objFolder.GetDetailsOf(item, 14).ToString();

     Console.WriteLine(file_version);

}

这个脚本结合了来自 http://nerdynotes.blogspot.com/2008/06/vbnet-shell32-code-compiled-on-vista.html

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

第二个链接是西班牙语的,我用谷歌翻译成英文的

感谢所有回答这个问题的人

【讨论】:

【解决方案2】:

【讨论】:

  • 在 stackoverflow 中发布问题之前,我已经尝试过该选项。我可以使用 object shell = Activator.CreateInstance(t); 创建一个对象;但是我不确定如何使用命名空间属性。我尝试按照您发布的链接的 cmets 中的建议使用它,但也没有运气。
【解决方案3】:

而不是

Type t = Type.GetTypeFromProgID("Shell.Application");

dynamic shell = Activator.CreateInstance(t);

我用过

var shell = (IShellDispatch4) new Shell();

shell.Namespace 然后按预期工作。

原来对 shell 对象的引用默认为 IShellDispatch5,不能在 XP 或 2003 中使用。

【讨论】:

  • 我无法在我的 C# 代码 (4.0) 中获得选项 IShellDispatch4。我需要添加任何参考吗?
猜你喜欢
  • 1970-01-01
  • 2012-07-16
  • 1970-01-01
  • 2012-06-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-27
  • 2012-01-29
相关资源
最近更新 更多