c# - C# - new Shell32() 抛出异常?
问题描述
我需要在我的 C# 应用程序中使用 Shell32 来创建一个 lnk 文件。
我将 shell32.dll 添加到我的引用中并尝试编译这个单一的代码行:
Shell32.Shell shell = new Shell32.Shell();
我得到了一个InvalidCastException
!
错误代码:'HRESULT: 0x80004002 (E_NOINTERFACE))。
我应该如何使用 Shell32.Shell?
解决方案
谢谢,我已经找到了您的评论的方法。我只需要将它用作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)
推荐阅读
- gitlab - 如何并行运行一些任务,而另一些则顺序运行?
- wpf - 带有代码签名证书的 Azure 通知中心和 UWP 应用(桌面桥)
- ruby-on-rails - 如何通过 Active Record 使用 SQL
- powershell - 如何比较两个变量之间的时间
- python - 如何在 Spark 的 Dataframe 中使用现有列作为索引
- android - Qt 应用程序在启动或从后台返回后在视觉上冻结
- sql - 加入查询结果指定的表/列
- c# - Mono winforms 无法呈现波斯文本
- r - pheatmap:提取行 z 得分值
- angular - Angular 7:无法读取未定义的属性“过滤器”