C# 如何在硬盘上获取目标文件的快捷方式?

C# 如何在硬盘上获取目标文件的快捷方式?,c#,C#,我正在尝试使用此功能,但出现了一些错误: public string GetShortcutTargetFile(string shortcutFilename) { string pathOnly = System.IO.Path.GetDirectoryName(shortcutFilename); string filenameOnly = System.IO.Path.GetFileName(shortcutFilename

我正在尝试使用此功能,但出现了一些错误:

public 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));
        }
第一个错误在该行:

Shell32.ShellLinkObject link = (Shell32.ShellLinkObject)folderItem.GetLink;
在行(Shell32.ShellLinkObject)的右侧folderItem.GetLink 我收到错误信息:

Error   2   One or more types required to compile a dynamic expression cannot be found. Are you missing references to Microsoft.CSharp.dll and System.Core.dll?
在最后的一行:

Console.WriteLine(GetShortcutTargetFile(path));
错误在:GetShortcutTargetFile(路径)上。函数是静态的,但我删除了静态的,然后在最后一行中设置了错误

Error   4   An object reference is required for the non-static field, method, or property 'GatherLinks.Form1.GetShortcutTargetFile(string)

如何修复所有错误以及如何获取目标文件的所有短路?

第一个错误:在项目设置中添加对Shell32.dll的引用

第二个错误:这两个函数放在哪里?似乎您正在尝试在表单中创建函数。这就是为什么您不能访问“GatherLinks.Form1.GetShortcutTargetFile(string)”。
将代码从主函数移动到表单加载事件,您将能够编译:)

方法是了解如何读取快捷方式文件。例如,把它们都放在列表中。谷歌可以很好地解决这两个错误。。。