【问题标题】:How to follow a .lnk file programmatically如何以编程方式跟踪 .lnk 文件
【发布时间】:2012-01-29 10:46:43
【问题描述】:

我们有一个充满指向文件夹的快捷方式(.lnk 文件)的网络驱动器,我需要在 C# Winforms 应用程序中以编程方式遍历它们。

我有哪些实用选项?

【问题讨论】:

  • 如果快捷方式不指向文件怎么办?
  • @vcsjones 如果没有,可以跳过。幸运的是,它已被锁定,只有网络工程师才能访问。
  • @DavidHeffernan 你知道 C# 中有什么免费的包装器吗?
  • 在谷歌中输入 c# ishelllink 并从那里获取

标签: c# .net windows winforms winapi


【解决方案1】:

添加 IWshRuntimeLibrary 作为对您项目的引用。添加引用、COM 选项卡、Windows 脚本宿主对象模型。

这是我获取快捷方式属性的方法:

IWshRuntimeLibrary.IWshShell wsh = new IWshRuntimeLibrary.WshShellClass();
IWshRuntimeLibrary.IWshShortcut sc = (IWshRuntimeLibrary.IWshShortcut)wsh.CreateShortcut(filename);

快捷方式对象“sc”有一个 TargetPath 属性。

【讨论】:

  • 感谢您的简单实用的建议和示例,不只是给我更多信息让我深入研究。我建议添加到您的答案中的一件事是您添加对 COM 对象“Windows 脚本宿主对象模型”的引用以获取 IWshRuntimeLibrary。
  • 只是为了让其他人不必在愚蠢的错误上浪费这么多时间:上面示例中的文件名当然应该是 file.FullName 如果读取 FileInfo,而不是 file.Name。
  • 我会补充一点,如果你得到一个'互操作类型'IWshRuntimeLibrary.WshShellClass'不能被嵌入。请改用适用的接口。 编译时出错,请在您的引用中选择 IWshRuntimeLibrary 并将“嵌入互操作类型”属性从 True 设置为 False。
【解决方案2】:

如果您不希望引用 COM,则将 Interop.IWshRuntimeLibrary.dll 与您的产品一起分发(记住 Jay Riggs “嵌入互操作类型”:False)

您可以改用新的动态 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;
        }
    }
}

【讨论】:

    【解决方案3】:
    1. 使用 COM IPersistFile 接口加载文件。
    2. 对结果执行 QueryInterface 以将其转换为 IShellLink 接口。
    3. 调用 IShellLink::GetPath

    据我所知,您可以让 .NET 使用“添加引用”对话框为您生成符合每个接口的类。

    【讨论】:

    • 谢谢。我要添加哪些文件作为参考?
    • @JonC:我不知道。我所知道的是,据说你可以添加一个 CoClass 作为参考。不是我自己做过的。
    【解决方案4】:

    IShellLink 接口让您可以操作 .lnk 文件,尽管在 C# 中使用它有点麻烦。

    This article 有一些代码实现了必要的互操作 gubbins。

    更新

    您可以从文章here 中找到代码,但该页面似乎无法在 Firefox 中运行。它确实在 IE 中工作。

    【讨论】:

    • 谢谢,但来源丢失了,他没有在博客中详细说明任何有用的内容。
    【解决方案5】:

    我知道这不是正确的方法,并且 lnk 文件结构可能会改变等等,但这就是我所做的:

        private static string LnkToFile(string fileLink)
        {
            string link = File.ReadAllText(fileLink);
            int i1 = link.IndexOf("DATA\0");
            if (i1 < 0)
                return null;
            i1 += 5;
            int i2 = link.IndexOf("\0", i1);
            if (i2 < 0)
                return link.Substring(i1);
            else
                return link.Substring(i1, i2 - i1);
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-09
      相关资源
      最近更新 更多