如何使用命令行参数在 C# 中创建应用程序快捷方式(.lnk 文件)
我编写了一个简单的基于 FTP 的文件同步程序。其设置可以存储在 XML 文件中。我希望用户能够使用各种配置快速打开程序。因此,我将程序设置为能够通过命令行参数读取配置文件的路径。这样就可以根据不同的配置文件在桌面上创建快捷方式。现在我正在尝试添加一项功能,该功能将自动创建特定配置文件的快捷方式。
我尝试使用 这篇文章使用 ShellLink.cs。我还尝试使用 IWshRuntimeLibrary,如此处。我可以创建快捷方式,但属性窗口中快捷方式的“目标类型”最终在新快捷方式中为空白。如果我双击该快捷方式,我会从 Windows 中收到一个错误窗口,提示在提供的路径中查找 FooBar.xml 时出现问题。所以它似乎没有意识到它应该启动应用程序而不是打开文件。
现在,如果我打开新快捷方式的属性并更改目标字段中的某些内容,然后将其更改回来。例如,从 xml 中删除 x,然后将其添加回来。单击“确定”后,快捷方式的图标立即切换到我的应用程序的图标,并且双击时可以正常工作。
因此,当我手动编辑快捷方式时,它似乎会强制 Windows 检查快捷方式目标类型是否应为“应用程序”并进行切换。
那么当目标路径不以.exe结尾时,如何通过C#创建目标类型为Application的新快捷方式呢?
I've written a simple FTP based file synchronization program. The settings of which can be stored in an XML file. I want users to be able to quickly open the program using various configurations. So I set up the program to be able to read the path to a config file through command line argument. This way shortcuts can be created on the desktop based on different config files. Now I'm trying to add a feature that will automatically create the shortcut for a specific config file.
I've tried using the example from this post using ShellLink.cs. And I've also tried using IWshRuntimeLibrary as described here. I'm able to create the shortcut, but the "Target Type" of the shortcut in the properties window ends up being blank in the new shortcut. If I double click the shortcut I get an error window from windows about it having problems finding FooBar.xml at the path provided. So it seems like it doesn't realize that it should be starting an application not opening a file.
Now, if I open the properties of the new shortcut and change something in the target field, and then change it back. For example delete the x from xml and then just add it back. After click OK the icon of the shortcut immediately switches to the icon of my app, and works correctly when double clicked.
So it appears that when I edit the shortcut manually it forces windows to check if the shortcut target type should be Application and switches it.
So how can I, through C#, create a new shortcut with a target type of Application when the target path does not end with .exe?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要添加对 Windows 脚本宿主的
COM
引用。 AFAIK,没有本地 .NET 方法可以做到这一点。此示例将在桌面上创建一个快捷方式。它将使用 XML 文件集作为命令行参数来启动 FTP 应用程序。只需将值更改为您需要的值即可。
使用此代码,目标类型将正确填充为应用程序,并将使用 settings1.xml 作为命令行参数启动应用程序。从你的问题来看,我认为这就是你想要做的。但是,我认为您无法创建一个 XML 文件的快捷方式,并将目标类型设置为应用程序。
对于 .NET 4.0 及更高版本,将第一行替换为以下内容 (感谢米莉·史密斯!):
You'll need to add a
COM
reference to Windows Scripting Host. AFAIK, there is no native .NET way to do this.This sample will create a shortcut on the desktop. It will launch the FTP app, using the XML file set as a command line argument. Just change the values to what you need.
With this code, the target type is properly populated as application and will launch the app with settings1.xml as a command line argument. From your question, I think this is what you are trying to do. However, I don't think you can create a shortcut to, let's say just an XML file, and have the target type set as application.
For .NET 4.0 and above, replace the first line with the following (Thanks to Millie Smith!):