C# 获取快捷方式文件夹的目标
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9414152/
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
Get target of shortcut folder
提问by Phu Minh Pham
How do you get the directory target of a shortcut folder? I've search everywhere and only finds target of shortcut file.
如何获取快捷方式文件夹的目录目标?我到处搜索,只找到快捷方式文件的目标。
采纳答案by Luke Girvin
I think you will need to use COM and add a reference to "Microsoft Shell Control And Automation", as described in this blog post:
我认为您需要使用 COM 并添加对“Microsoft Shell 控制和自动化”的引用,如本博客文章所述:
Here's an example using the code provided there:
这是使用那里提供的代码的示例:
namespace Shortcut
{
using System;
using System.Diagnostics;
using System.IO;
using Shell32;
class Program
{
public static string GetShortcutTargetFile(string shortcutFilename)
{
string pathOnly = System.IO.Path.GetDirectoryName(shortcutFilename);
string filenameOnly = System.IO.Path.GetFileName(shortcutFilename);
Shell shell = new Shell();
Folder folder = shell.NameSpace(pathOnly);
FolderItem folderItem = folder.ParseName(filenameOnly);
if (folderItem != null)
{
Shell32.ShellLinkObject link = (Shell32.ShellLinkObject)folderItem.GetLink;
return link.Path;
}
return string.Empty;
}
static void Main(string[] args)
{
const string path = @"C:\link to foobar.lnk";
Console.WriteLine(GetShortcutTargetFile(path));
}
}
}
回答by TwistedMexi
All file shortcuts have a .lnk file extension you can check for. Using a string for example, you could use string.EndsWith(".lnk") as a filter.
所有文件快捷方式都有一个 .lnk 文件扩展名,您可以检查。例如,使用字符串,您可以使用 string.EndsWith(".lnk") 作为过滤器。
All URL shortcuts have a .url file extension, so you will need to account for those as well if needed.
所有 URL 快捷方式都有一个 .url 文件扩展名,因此如果需要,您还需要考虑这些。
回答by Suhan
If you don't want to use dependencies you can use https://blez.wordpress.com/2013/02/18/get-file-shortcuts-target-with-c/But lnk format is undocumented, so do it only if you understand risks.
如果您不想使用依赖项,您可以使用https://blez.wordpress.com/2013/02/18/get-file-shortcuts-target-with-c/但是 lnk 格式没有记录,所以只做如果您了解风险。
回答by Mohsen.Sharify
if you want find your application path that has shortcut on desktop, an easy way that i use, is the following:
如果您想在桌面上找到具有快捷方式的应用程序路径,我使用的一种简单方法如下:
Process.GetCurrentProcess().MainModule.FileName.Substring(0, Process.GetCurrentProcess().MainModule.FileName.LastIndexOf("\")
this code return any exe path that is running,Regardless that who requested file
此代码返回任何正在运行的 exe 路径,无论谁请求文件
回答by Meine
An even simpler way to get the linked path that I use is:
获取我使用的链接路径的更简单方法是:
private static string LnkToFile(string fileLink)
{
string link = File.ReadAllText(fileLink);
int i1 = link.IndexOf("DATA// new way for windows 10
string targetname;
string pathOnly = System.IO.Path.GetDirectoryName(LnkFileName);
string filenameOnly = System.IO.Path.GetFileName(LnkFileName);
Shell shell = new Shell();
Shell32.Folder folder = shell.NameSpace(pathOnly);
FolderItem folderItem = folder.ParseName(filenameOnly);
if (folderItem != null) {
Shell32.ShellLinkObject link = (Shell32.ShellLinkObject)folderItem.GetLink;
targetname = link.Target.Path; // <-- main difference
if (targetname.StartsWith("{")) { // it is prefixed with {54A35DE2-guid-for-program-files-x86-QZ32BP4}
int endguid = targetname.IndexOf("}");
if (endguid > 0) {
targetname = "C:\program files (x86)" + targetname.Substring(endguid + 1);
}
}
");
if (i1 < 0)
return null;
i1 += 5;
int i2 = link.IndexOf("var fileName = Process.GetCurrentProcess().MainModule.FileName;
var folderName = Path.Combine(fileName, ".."); //origin folder
", i1);
if (i2 < 0)
return link.Substring(i1);
else
return link.Substring(i1, i2 - i1);
}
But it will of course break if the lnk-file format changes.
但如果 lnk 文件格式发生变化,它当然会中断。
回答by EJ Thayer
In windows 10 it needs to be done like this, first add COM reference to "Microsoft Shell Control And Automation"
在windows 10中需要这样做,首先将COM引用添加到“Microsoft Shell Control And Automation”
##代码##回答by Barabas
Thanks to Mohsen.Sharify's answerI got more neat piece of code:
感谢Mohsen.Sharify 的回答,我得到了更简洁的代码:
##代码##
