windows 如何以编程方式跟踪 .lnk 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8660705/
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
How to follow a .lnk file programmatically
提问by user1231231412
We have a network drive full of shortcuts (.lnk files) that point to folders and I need to traverse them programmatically in a C# Winforms app.
我们有一个充满指向文件夹的快捷方式(.lnk 文件)的网络驱动器,我需要在 C# Winforms 应用程序中以编程方式遍历它们。
What practicaloptions do I have?
我有哪些实用的选择?
回答by djdanlib
Add IWshRuntimeLibrary as a reference to your project. Add Reference, COM tab, Windows Scripting Host Object Model.
添加 IWshRuntimeLibrary 作为对项目的引用。添加引用、COM 选项卡、Windows 脚本宿主对象模型。
Here is how I get the properties of a shortcut:
这是我获取快捷方式属性的方法:
IWshRuntimeLibrary.IWshShell wsh = new IWshRuntimeLibrary.WshShellClass();
IWshRuntimeLibrary.IWshShortcut sc = (IWshRuntimeLibrary.IWshShortcut)wsh.CreateShortcut(filename);
The shortcut object "sc" has a TargetPath property.
快捷方式对象“sc”具有 TargetPath 属性。
回答by Michael
If you do not wish to reference COM, and distribute the Interop.IWshRuntimeLibrary.dll with your product (remembering Jay Riggs "Embed Interop Types": False)
如果您不想引用 COM,并且不希望随产品一起分发 Interop.IWshRuntimeLibrary.dll(记住 Jay Riggs“嵌入互操作类型”:错误)
You can use the new dynamic COM instead.
您可以改用新的动态 COM。
private void Window_Drop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
dynamic shortcut;
dynamic windowsShell;
try
{
var file = files[0];
if (Path.GetExtension(file)?.Equals(".lnk",StringComparison.OrdinalIgnoreCase) == true)
{
Type shellObjectType = Type.GetTypeFromProgID("WScript.Shell");
windowsShell = Activator.CreateInstance(shellObjectType);
shortcut = windowsShell.CreateShortcut(file);
file = shortcut.TargetPath;
// Release the COM objects
shortcut = null;
windowsShell = null;
}
//
// <use file>...
//
}
finally
{
// Release the COM objects
shortcut = null;
windowsShell = null;
}
}
}
回答by Billy ONeal
- Load the file using the COM IPersistFile interface.
- Do a QueryInterface on the result to turn it into an IShellLink interface.
- Call IShellLink::GetPath
- 使用 COM IPersistFile 接口加载文件。
- 对结果执行 QueryInterface 以将其转换为 IShellLink 接口。
- 调用 IShellLink::GetPath
As far as I am aware you can have .NET generate classes conforming to each of these interfaces for you using the "Add Reference" dialog box.
据我所知,您可以使用“添加引用”对话框让 .NET 为您生成符合这些接口中的每一个的类。
回答by arx
The IShellLink interface lets you manipulate .lnk files, though it's a bit of a pain to use from C#.
IShellLink 接口允许您操作 .lnk 文件,尽管从 C# 使用它有点痛苦。
This articlehas some code implementing the necessary interop gubbins.
本文有一些代码实现了必要的互操作 gubbins。
Update
更新
You can find the code from the article herebut the page doesn't seem to work in Firefox. It does work in IE.
您可以在此处找到文章中的代码,但该页面在 Firefox 中似乎不起作用。它在 IE 中确实有效。
回答by Meine
I know it is not the correct way and that lnk file structures can change etc., but this is what I do:
我知道这不是正确的方法,lnk 文件结构可以改变等等,但这就是我所做的:
private static string LnkToFile(string fileLink)
{
string link = File.ReadAllText(fileLink);
int i1 = link.IndexOf("DATA##代码##");
if (i1 < 0)
return null;
i1 += 5;
int i2 = link.IndexOf("##代码##", i1);
if (i2 < 0)
return link.Substring(i1);
else
return link.Substring(i1, i2 - i1);
}