我正在使用 C# 访问系统上的最新文件并通过
Environment.SpecialFolder.Recent
然而,Windows 中的最近文件夹只是创建文件实际位置的快捷方式。如何复制快捷方式指向的文件而不是快捷方式本身?
非常感谢您的帮助
我正在使用 C# 访问系统上的最新文件并通过
Environment.SpecialFolder.Recent
然而,Windows 中的最近文件夹只是创建文件实际位置的快捷方式。如何复制快捷方式指向的文件而不是快捷方式本身?
非常感谢您的帮助
我发现并修改了这段代码,对我有用:
static string GetShortcutTargetFile(string shortcutFilename)
{
string pathOnly = System.IO.Path.GetDirectoryName(shortcutFilename);
string filenameOnly = System.IO.Path.GetFileName(shortcutFilename);
Shell32.Shell shell = new Shell32.Shell();
Shell32.Folder folder = shell.NameSpace(pathOnly);
Shell32.FolderItem folderItem = folder.ParseName(filenameOnly);
if (folderItem != null)
{
return ((Shell32.ShellLinkObject)folderItem.GetLink).Path;
}
return ""; // not found, use if (File.Exists()) in the calling code
// or remove the return and throw an exception here if you like
}
您必须在Microsoft Shell Controls And Automation
项目中添加对 COM 对象 (Shell32.dll) 的引用才能使其工作。
这是不久前提出的类似问题,但第一个答案提供了一些代码,可以解决目标文件/文件夹名称的问题。
从那里,您只需浏览您的快捷方式列表,解析它们的链接位置,然后使用File.Copy(linkPath, copyPath);
来完成工作。
也许这个示例代码可以帮助您从 .lnk 文件中获取目标链接。