5

回到 .NET 1.0 天,我写了一个方法来返回 MS Windows 上快捷方式的目标。它通过使用与 Windows 脚本宿主对象模型的互操作并通过 COM 接口强制执行此操作:

private FileInfo GetFileFromShortcut(FileInfo shortcut)
{
    FileInfo targetFile = null;

    try
    {
        IWshRuntimeLibrary.WshShell wShell = new IWshRuntimeLibrary.WshShellClass();
        IWshRuntimeLibrary.WshShortcut wShortcut = (IWshRuntimeLibrary.WshShortcut)wShell.CreateShortcut(shortcut.FullName);

        // if the file wasn't a shortcut then the TargetPath comes back empty
        string targetName = wShortcut.TargetPath;
        if (targetName.Length > 0)
        {
            targetFile = new FileInfo(targetName);
        }
    }
    catch (Exception)
    { // will return a null targetFile if anything goes wrong
    }

    return targetFile;
}

这仍然困扰着我,我希望用更优雅的东西来替换它,但前提是替换实际上至少也能正常工作。我仍然找不到找到快捷方式目标的原生 C# 方法。有没有,或者这仍然是做这类事情的最佳方式?

4

3 回答 3

1

你不能只打开 .lnk 或 .url 文件并解析它吗?

这谈到了同样的事情并显示了文件的样子: http ://www.programmingtalk.com/showthread.php?t=7335

于 2009-03-23T00:39:20.687 回答
1

看起来有人在 C# 中编写了一个名为ShellLink的类来操作快捷方式文件,但它也使用了 COM。

于 2009-03-11T21:16:07.897 回答
0

不久前我也对这个感兴趣。

这是已接受的响应,其中包含指向LNK 文件格式的(非正式)描述的链接。显然,所有可用的方法需要通过一些 API。

于 2010-11-22T05:31:13.467 回答