2012-11-18 53 views
4

在Windows中,無論是在桌面上還是在Windows資源管理器中,我都希望檢測選擇(高亮顯示)文件或文件夾的時刻。發生這種情況時,我想顯示一個消息框,顯示文件或文件夾的全名。在Windows資源管理器中檢測文件選擇

如果選擇了多個項目,我想顯示所有項目。

請注意,我的解決方案必須用C#編寫。

+0

你讀過[這](http://stackoverflow.com/questions/4921413/get-selected-file-or-folder-in-windows-explorer)或[此](http://stackoverflow.com/questions/3382946/get-selected-items-of-folder-with-winapi/3400348#3400348)? – khellang

+0

thanyou,khellang,是的,我讀過他們,但我仍然困惑。我需要一個清晰的C#代碼。 – ohsorry

+0

im試試這個:IShellFolderViewDual2 – ohsorry

回答

3

這個例子來看看,以獲得鼠標點擊或選擇的事件:

https://stackoverflow.com/questions/7222749/i-created-a-program-to-hide-desktop-icons-on-double-click-of-desktop-but-would-o

加入與下面的代碼,只要加入引用shdocvw.dll中和的Shell32.dll這將返回所有在每個瀏覽器中選擇的項目和文件夾路徑。

public void GetListOfSelectedFilesAndFolderOfWindowsExplorer() 
    { 
     string filename; 
     ArrayList selected = new ArrayList(); 
     var shell = new Shell32.Shell(); 
     //For each explorer 
     foreach (SHDocVw.InternetExplorer window in new SHDocVw.ShellWindowsClass()) 
     { 
      filename = Path.GetFileNameWithoutExtension(window.FullName).ToLower(); 
      if (filename.ToLowerInvariant() == "explorer") 
      { 
       Shell32.FolderItems items = ((Shell32.IShellFolderViewDual2)window.Document).SelectedItems(); 
       foreach (Shell32.FolderItem item in items) 
       { 
        MessageBox.Show(item.Path.ToString()); 
        selected.Add(item.Path); 
       } 
      } 
     } 
    } 
0

只是增加出頭到Renier的回答是:

  • shdocvw.dll中和的Shell32.dll所在的文件夾C:\ Windows \ System32下
  • 如果您在SHDOCVW出現錯誤。 ShellWindowsClass()只需在解決方案資源管理器上右鍵單擊SHDocVw參考,然後選擇屬性並設置 嵌入互操作類型
相關問題