我正在开发需要发送快捷方式到桌面操作的应用程序。我发现只有一种方法可以实现它:
var shell = new IWshRuntimeLibrary.WshShell();
var shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(linkFileName);
shortcut.TargetPath = Application.ExecutablePath;
shortcut.WorkingDirectory = Application.StartupPath;
shortcut.Save();它是工作核心,但需要Interop.IWshRuntimeLibrary.dll。我的应用程序需要通过一个小的exe文件进行部署,我不能将任何其他文件包含到包中。
哪里是调用COM而不使用互操作dll的方法?
发布于 2011-07-25 02:05:53
是的,您可以在没有互操作库的情况下在.NET中创建COM对象。只要它们的COM对象实现了IDispatch ( WScript.Shell就实现了),您就可以很容易地在它上面调用方法和属性。
如果您使用的是.NET 4,则动态类型使这一点变得非常容易。如果不是这样,你将不得不使用反射来调用这些方法,这将会起作用,但并不美观。
具有动态的.NET 4
Type shellType = Type.GetTypeFromProgID("WScript.Shell");
dynamic shell = Activator.CreateInstance(shellType);
dynamic shortcut = shell.CreateShortcut(linkFileName);
shortcut.TargetPath = Application.ExecutablePath;
shortcut.WorkingDirectory = Application.StartupPath;
shortcut.Save();带有反射的.NET 3.5或更早版本
Type shellType = Type.GetTypeFromProgID("WScript.Shell");
object shell = Activator.CreateInstance(shellType);
object shortcut = shellType.InvokeMember("CreateShortcut",
BindingFlags.Public | BindingFlags.Instance | BindingFlags.InvokeMethod,
null, shell, new object[] { linkFileName });
Type shortcutType = shortcut.GetType();
shortcutType.InvokeMember("TargetPath",
BindingFlags.Public | BindingFlags.Instance | BindingFlags.SetProperty,
null, shortcut, new object[] { Application.ExecutablePath });
shortcutType.InvokeMember("WorkingDirectory",
BindingFlags.Public | BindingFlags.Instance | BindingFlags.SetProperty,
null, shortcut, new object[] { Application.StartupPath });
shortcutType.InvokeMember("Save",
BindingFlags.Public | BindingFlags.Instance | BindingFlags.InvokeMethod,
null, shortcut, null);发布于 2011-07-25 01:19:12
您可以使用可从here下载的名为ILMerge的微软实用程序将DLL合并到您的exe中。
下面是关于如何在this article中使用它的简要说明
发布于 2011-07-25 01:33:27
好吧,由于某些原因,ILMerge是不可接受的。
C#包括与基础COM和Windows交互的能力。您可以在EXE中编写自己的COM互操作。
下面的an article描述了如何做到这一点。
https://stackoverflow.com/questions/6808369
复制相似问题