C# 创建文件快捷方式 (.lnk)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18023379/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 11:05:23  来源:igfitidea点击:

Creating a file shortcut (.lnk)

c#automationshortcutlnk

提问by user2599803

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..

我一直在寻找一种在 C# 中创建文件快捷方式的简单方法,但我只找到了执行此操作的外部 dll。这实际上很令人惊讶,没有内置的方法可以做到这一点..

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.

无论如何,我知道 lnk 文件只是具有特定命令和给定路径的文本文件。我想也许我可以创建一个文本文件(在代码中)将其文本设置为正确的命令并将其扩展名更改为 .lnk 我曾尝试先手动执行此操作,但未能这样做。

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

有没有办法做类似的事情(或者可能是另一种简单的方法)来创建指向 c# 中某个路径的快捷方式?

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

为了清楚起见,快捷方式我的意思是一个 .lnk 文件,它导致文件编辑:而文件我的意思是我想要的任何文件,不仅是我自己的应用程序的快捷方式



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
  1. Microsoft Shell 控件和自动化
  2. Windows 脚本宿主对象模型

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.

另外,如果有一种方法可以创建文件,编写正确的命令,然后将其更改为 lnk 文件,请告诉我。

回答by Danilo Breda

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

Joepro 在他们的回答中指出了一种方法:

You'll need to add a COM reference to Windows Scripting Host. As far as i know, 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();

您需要添加对 Windows Scripting Host 的 COM 引用。据我所知,没有原生的 .net 方法可以做到这一点。

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:

对于 .Net 4.0 及更高版本,将第一行替换为以下内容:

 WshShell wsh = new WshShell();

EDIT:This link can help too

编辑:此链接也可以提供帮助