有没有办法在不使用COM的情况下在C#中以编程方式遵循Windows文件系统快捷方式?

Ala*_*ett 5 c# windows shortcuts

回到.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;
}
Run Code Online (Sandbox Code Playgroud)

这仍然让我感到困扰,我希望用更优雅的东西取代它,但前提是替换实际上至少也是如此.我仍然找不到寻找快捷方式目标的本地C#方式.有没有,或者这仍然是做这种事情的最佳方式?

Pow*_*ord 1

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