2014-05-14 50 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

相关问题