2014-05-14 52 views
0

我在我的一個項目中需要從其路徑中獲取特定文件夾的圖標。如何獲取與特定文件夾關聯的圖標?

例如:
如果我使用C:\Users\Username\Desktop我想如果我使用的是具有自定義圖標文件夾的路徑與桌面文件夾
關聯的圖標,我想那個圖標

不,我不想要一般默認文件夾圖標

我一直在尋找近3天,沒有運氣。任何幫助表示讚賞。

+1

骯髒的方法是從desktop.ini抓住它,但由於它很明顯(因爲它是文件夾中的jst文件),我假設你正在尋找其他東西...這裏是現有的相同類型的問題:http ://stackoverflow.com/questions/tagged/desktop.ini(不知道你花了3天的時間)。而這[獲取文件夾類型](http://stackoverflow.com/questions/17158300/how-to-get-set-folder-type-in​​-c-sharp)可能包含您需要開始編寫解決方案的所有信息。 –

回答

3

您可以使用本機SHGetFileInfo函數。您可以使用.net InteropServices導入它。

有一篇知識庫文章描述瞭如何做到這一點。

http://support.microsoft.com/kb/319350

編輯:

另外

How do I fetch the folder icon on Windows 7 using Shell32.SHGetFileInfo

實施例:

using System; 
using System.Drawing; 
using System.Runtime.InteropServices; 
using System.Windows; 
using System.Windows.Interop; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 

namespace IconTest2 
{ 
    public partial class MainWindow : Window 
    { 
     //Struct used by SHGetFileInfo function 
     [StructLayout(LayoutKind.Sequential)] 
     public struct SHFILEINFO 
     { 
      public IntPtr hIcon; 
      public int iIcon; 
      public uint dwAttributes; 
      [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)] 
      public string szDisplayName; 
      [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)] 
      public string szTypeName; 
     }; 

     //Constants flags for SHGetFileInfo 
     public const uint SHGFI_ICON = 0x100; 
     public const uint SHGFI_LARGEICON = 0x0; // 'Large icon 

     //Import SHGetFileInfo function 
    [DllImport("shell32.dll")] 
    public static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags); 

      public MainWindow() 
      { 
       InitializeComponent(); 
       SHFILEINFO shinfo = new SHFILEINFO(); 

       //Call function with the path to the folder you want the icon for 
       SHGetFileInfo(
        "C:\\Users\\Public\\Music", 
        0, ref shinfo,(uint)Marshal.SizeOf(shinfo), 
        SHGFI_ICON | SHGFI_LARGEICON); 

       using (Icon i = System.Drawing.Icon.FromHandle(shinfo.hIcon)) 
       { 
        //Convert icon to a Bitmap source 
        ImageSource img = Imaging.CreateBitmapSourceFromHIcon(
              i.Handle, 
              new Int32Rect(0, 0, i.Width, i.Height), 
              BitmapSizeOptions.FromEmptyOptions()); 

        //WPF Image control 
        m_image.Source = img; 
       } 
      } 
     } 
    } 

參考對的SHGetFileInfo - http://msdn.microsoft.com/en-us/library/windows/desktop/bb762179(v=vs.85).aspx

+0

良好的鏈接 - 確保提供小的摘要(可能只是調用的樣本),以使答案符合SO標準。 –

+1

@AlexeiLevenkov已更新,例如 – Wearwolf

+0

感謝兄弟,我會試試這個,如果它有效,請將它標記爲答案:D – vbtheory

相關問題