如何创建在C#中的应用程序快捷方式(.lnk文件)的命令行参数 [英] How do you create an application shortcut (.lnk file) in C# with command line arguments

查看:1157
本文介绍了如何创建在C#中的应用程序快捷方式(.lnk文件)的命令行参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个简单的基于FTP文件同步程序。其中的设置可以存储在XML文件中。我希望用户能够快速打开使用不同的配置方案。所以,我成立了该计划,使其能够读取路径,通过命令行参数的配置文件。这种方式的快捷方式可以在桌面上根据不同的配置文件来创建。现在,我想添加一个功能,它会自动创建一个特定的配置文件的快捷方式。

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.

我一直使用从<一个例子尝试href=\"http://stackoverflow.com/questions/234231/how-do-you-create-an-application-shortcut-lnk-file-in-c-or-net\">this使用ShellLink.cs帖子。而且我也试过用IWshRuntimeLibrary描述这里。我能够创建快捷方式,但是在属性窗口快捷方式的目标类型最终被新的快捷方式的空白。如果我双击该快捷方式,我得到大约具有提供的路径找到FooBar.xml问题是Windows的故障窗口。因此,现在看来似乎并没有意识到它应该启动应用程序不能打开一个文件。

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.

现在,如果我打开新的快捷方式的属性,并更改目标字段的东西,然后再改回来。例如删除XML的X,然后只需添加回来。单击确定后的快捷图标立即切换到我的应用程序的图标,并且能够正常运行双点击时。

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.

所以,我怎样才能通过C#,创建应用程序的目标类型的新的快捷方式,当目标路径不以.exe结束?

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?

推荐答案

您需要添加COM引用到Windows脚本宿主。据我所知,没有这样做没有原生的.NET方法。

You'll need to add a COM reference to Windows Scripting Host. AFAIK, there is no native .net way to do this.

此样本将在桌面上创建一个快捷方式。这将启动FTP应用程序,使用设置为一个命令行参数的XML文件。只要改变这些值你所需要的。

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.

        WshShellClass wsh = new WshShellClass();
        IWshRuntimeLibrary.IWshShortcut shortcut = wsh.CreateShortcut(
            Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\shorcut.lnk") as IWshRuntimeLibrary.IWshShortcut;
        shortcut.Arguments = "c:\\app\\settings1.xml";
        shortcut.TargetPath = "c:\\app\\myftp.exe";
        // not sure about what this is for
        shortcut.WindowStyle = 1; 
        shortcut.Description = "my shortcut description";
        shortcut.WorkingDirectory = "c:\\app";
        shortcut.IconLocation = "specify icon location";
        shortcut.Save();

有了这个code,目标类型是正确填充应用和将推出的应用程序与settings1.xml作为命令行参数。从你的问题,我认为这是你正在尝试做的。不过,我不认为你可以创建一个快捷方式,让我们说只是一个XML文件,并有目标类型设置为应用程序。

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.

对于以上.NET 4.0,替换为下面第一行(感谢米莉·史密斯!):

        WshShell wsh = new WshShell();

这篇关于如何创建在C#中的应用程序快捷方式(.lnk文件)的命令行参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆