【问题标题】:Copying files behind shortcuts in C#在 C# 中复制快捷方式后面的文件
【发布时间】:2011-12-06 01:48:38
【问题描述】:

我正在使用 C# 访问系统上的最新文件并通过

复制它们
Environment.SpecialFolder.Recent

然而,Windows 中的最近文件夹只是创建文件实际位置的快捷方式。如何复制快捷方式指向的文件而不是快捷方式本身?

非常感谢您的帮助

【问题讨论】:

标签: c# file copy shortcut file-copying


【解决方案1】:

我发现并修改了这段代码,对我有用:

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) 的引用才能使其工作。

【讨论】:

【解决方案2】:

这是不久前提出的类似问题,但第一个答案提供了一些代码,可以解决目标文件/文件夹名称的问题。

How to resolve a .lnk in c#

从那里,您只需浏览您的快捷方式列表,解析其链接位置,然后使用File.Copy(linkPath, copyPath); 即可完成工作。

【讨论】:

    【解决方案3】:

    也许this sample code 可以帮助您从 .lnk 文件中获取目标链接。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-22
      • 2012-10-12
      • 1970-01-01
      • 2021-12-15
      • 2022-11-04
      • 1970-01-01
      • 2019-10-02
      • 1970-01-01
      相关资源
      最近更新 更多