2011-10-12 70 views
3

我使用C#来访问系统上的最近使用的文件,并通过复制文件在C#

Environment.SpecialFolder.Recent 

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

提前许多感谢所有帮助

+0

[如何在C#中解析.lnk]可能的重复(http://stackoverflow.com/questions/139010/how-to-resolve-a-lnk-in-c) – ChrisF

回答

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中)到该项目使这项工作。

+0

“Shell32”在哪里BCL? – spender

+1

不是,请参阅编辑。 – CodeCaster

+1

似乎合理,但请注意需要管理员权限才能访问的对象的问题。有关详细信息,请参阅http://stackoverflow.com/questions/2934420/why-do-i-get-e-accessdenied-when-reading-public-shortcuts-through-shell32。 – corvuscorax

0

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

How to resolve a .lnk in c#

从那里,你只会通过你的快捷键列表,解决他们的链接的位置,并使用File.Copy(linkPath, copyPath);来完成这项工作。