在 C# 中复制快捷方式后面的文件

发布于 2024-12-09 13:39:15 字数 175 浏览 1 评论 0原文

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

Environment.SpecialFolder.Recent

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

非常感谢您的帮助

I am using C# to access the most recent files on a system and copy them via

Environment.SpecialFolder.Recent

The recent folder in Windows however just creates shortcuts to the actual location of the file. How would one go about copying the file that the shortcut is pointing at as opposed to the shortcut itself?

Many Thanks in advance for any help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

习惯成性 2024-12-16 13:39:15

我找到并修改了这段代码,对我有用:

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) 的引用添加到项目中才能使其工作。

I found and altered this code, works for me:

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
}

You have to add a reference to the Microsoft Shell Controls And Automation COM object (Shell32.dll) to the project to make this work.

日暮斜阳 2024-12-16 13:39:15

这是不久前提出的一个类似的问题,但第一个答案提供了一些代码,这些代码可以执行您想要的操作,从而解析目标文件/文件夹名称。

如何在 c# 中解析 .lnk

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

This is a similar question that was asked a while ago, but the first answer provides some code that will do what you want as far as resolving the target file / folder name.

How to resolve a .lnk in c#

From there, you would simply go through your list of shortcuts, resolve their linked location, and use File.Copy(linkPath, copyPath); to finish the job.

夜血缘 2024-12-16 13:39:15

也许这个示例代码可以帮助您从.lnk 文件。

Maybe this sample code can help you to get the target link out of a .lnk file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文