我尝试使用此功能但遇到一些错误:
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(path) 该函数是静态的,但我删除了静态,然后我在最后一行显示错误。
Error 4 An object reference is required for the non-static field, method, or property 'GatherLinks.Form1.GetShortcutTargetFile(string)
如何修复所有错误以及如何获取目标文件的所有缺点?