2011-10-12 67 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);來完成這項工作。