首页 > 解决方案 > C# - new Shell32() 抛出异常?

问题描述

我需要在我的 C# 应用程序中使用 Shell32 来创建一个 lnk 文件。

我将 shell32.dll 添加到我的引用中并尝试编译这个单一的代码行:

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

我得到了一个InvalidCastException

错误代码:'HRESULT: 0x80004002 (E_NOINTERFACE))。

我应该如何使用 Shell32.Shell?

标签: c#shell32shell32.dll

解决方案


谢谢,我已经找到了您的评论的方法。我只需要将它用作dynamic.

static readonly Guid CLSID_Shell = Guid.Parse("13709620-C279-11CE-A49E-444553540000");
dynamic shell = Activator.CreateInstance(Type.GetTypeFromCLSID(CLSID_Shell));

另外,如果我将其添加STAThreadAttribute到我的 Main 方法中,它可以正常工作(归功于@Matthew Watson


推荐阅读