创建文件快捷方式(.lnk) [英] Creating a file shortcut (.lnk)

查看:389
本文介绍了创建文件快捷方式(.lnk)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找一种简单的方法来创建一个快捷方式在C#中的文件,但我只发现做外部DLL。
它实际上是相当惊人的,没有内置的方法来做到这一点。

反正我知道LNK文件只是文本具有一定的命令和给定路径的文件。
我想,也许我可以创建一个文本文件(在code)设置它的文本向右命令,并改变它的扩展名的.lnk
我想这样做手工第一,但未能如愿。

有没有办法做这样的事情(或者另一种简单的方式)在C#中创建一个快捷方式到一定的路径?

只是要清楚,通过快捷键我的意思是导致文件的.lnk文件
编辑:和按文件我的意思是任何文件,我会想,不仅是一个快捷方式到我自己的应用程序


如果它不为每个场景中很好地工作,我会编辑。

添加这些引用:


  1. 微软壳牌控制和自动化

  2. Windows脚本宿主对象模型

将此命名空间:

 使用SHELL32;
使用IWshRuntimeLibrary;

接下来似乎是工作:

  VAR WSH =新IWshShell_Class();
IWshRuntimeLibrary.IWshShortcut快捷= wsh.CreateShortcut(
    Environment.GetFolderPath(Environment.SpecialFolder.Desktop)+\\\\ shorcut2.lnk)作为IWshRuntimeLibrary.IWshShortcut;
shortcut.TargetPath = @C:\\用户\\子敏\\桌面\\ test文件夹;
shortcut.Save();

希望它可以帮助别人为好,感谢您的关注。

此外,如果有一种方法可以创建一个文件,写正确的命令,然后将其更改为LNK文件时,请让我知道。


解决方案

指出这样做的一个出路通过Joepro在<一个href=\"http://stackoverflow.com/questions/1501608/how-do-you-create-an-application-shortcut-lnk-file-in-c-sharp-with-command-li/1501727#1501727\">their回答这里:


  

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

  WshShellClass WSH =新WshShellClass();
IWshRuntimeLibrary.IWshShortcut快捷= wsh.CreateShortcut(
    Environment.GetFolderPath(Environment.SpecialFolder.Desktop)+\\\\ shorcut.lnk)作为IWshRuntimeLibrary.IWshShortcut;
shortcut.Arguments =;
shortcut.TargetPath =C:\\\\ \\\\应用myftp.exe
//不知道什么这是
shortcut.WindowStyle = 1;
shortcut.Description =我的快捷键说明;
shortcut.WorkingDirectory =C:\\\\应用;
shortcut.IconLocation =指定图标的位置;
shortcut.Save();


有关.NET 4.0及以上,用以下内容替换第一行:

 的WshShell WSH =新的WshShell();

编辑:
这个链接可以帮助过

I have been looking for a simple way to create a shortcut to a file in C#, but I've only found external dlls that do that. It's actually quite surprising, there's no built in way to do that..

Anyway, I know that lnk files are just text files with a certain command and a given path. I thought that maybe I could create a text file (in the code) set it's text to the right command and change it's extension to .lnk I've tried to do that manually first, but failed to do so.

Is there a way to do something like that (or maybe another simple way) to create a shortcut to a certain path in c#?

Just to be clear, by Shortcut I mean an .lnk file that leads to the file Edit: And by file I mean any file I'd want, not only a shortcut to my own application


I'll edit if it doesn't work well for every scenario.

Add these references:

  1. Microsoft Shell Controls And Automation
  2. Windows Script Host Object Model

Add this namespaces:

using Shell32;
using IWshRuntimeLibrary;

Next appears to be working:

var wsh = new IWshShell_Class();
IWshRuntimeLibrary.IWshShortcut shortcut = wsh.CreateShortcut(
    Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\shorcut2.lnk") as IWshRuntimeLibrary.IWshShortcut;
shortcut.TargetPath = @"C:\Users\Zimin\Desktop\test folder";            
shortcut.Save();

Hope it helps others as well, thanks for your attention.

Also, if there IS a way to create a file, write the right commands and then change it to an lnk file, please let me know.

解决方案

One way to do this is pointed out by Joepro in their answer here:

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

WshShellClass wsh = new WshShellClass();
IWshRuntimeLibrary.IWshShortcut shortcut = wsh.CreateShortcut(
    Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\shorcut.lnk") as IWshRuntimeLibrary.IWshShortcut;
shortcut.Arguments = "";
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();

For .Net 4.0 and above, replace the first line with the following:

 WshShell wsh = new WshShell();

EDIT: This link can help too

这篇关于创建文件快捷方式(.lnk)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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