none
需要在程序中获取资源管理器中的文件和文件夹的图标,请问如何获取? RRS feed

  • 问题

  • ​如下面的图

    需要在程序中获取资源管理器中的文件和文件夹的图标,请问如何获取?

    谢谢。

    2013年9月11日 9:18

答案

  • 使用shell32.dll里面的ExtractAssociateIconA方法来获取看看,示例代码如下:

    using System;
    using System.Runtime.InteropServices;
    namespace CGetIcon
    {
    	/// <summary>
    	/// This class is used to extract Icons from Executables and file types.
    	/// </summary>
    	/// <remarks>Designed by Daniel Romano</remarks>
    	public class IconExtractor
    	{
    		/// <summary>
    		/// This is the declararion of the DestroyIcon Windows API call from User32.dll
    		/// </summary>
    		/// <param name="hicon">The handle for the icon that will be disposed by the function.</param>
    		/// <returns></returns>
    		[DllImport("User32.DLL")] private static extern int DestroyIcon(int hicon);
    		/// <summary>
    		/// This is the declaration of the ExtractIcon Windows API call from shell32.dll.
    		/// This call can only be used with Library files (.exe, .dll, .ico).
    		/// </summary>
    		/// <param name="lpszFile">(in) The fully qualified file name of the library</param>
    		/// <param name="nIconIndex">(in) The index number position of the icon within the library</param>
    		/// <param name="phiconLarge">(out) This returns the handle of the Large Icon (32x32)</param>
    		/// <param name="phiconSmall">(out) This returns the handle of the Small Icon (16x16)</param>
    		/// <param name="nIcon">(in) The number of Icons to be extracted from the library.</param>
    		/// <returns>The handle for the extracted icons.</returns>
    		[DllImport("shell32.DLL", EntryPoint = "ExtractIconEx")] private static extern int ExtractIconExA(string lpszFile,int nIconIndex,ref int phiconLarge,ref int phiconSmall,int nIcon);
    		/// <summary>
    		/// This is the declaration of the ExtractAssociatedIcon Windows API call from shell32.dll
    		/// This call can only be used with non-library files (eg: .doc | Word Document File)
    		/// </summary>
    		/// <param name="hInst">(in) Handle of the application that is requesting the Icon.</param>
    		/// <param name="lpIconPath">(in) Path to the file that is Associated with the Icon.</param>
    		/// <param name="lpiIcon">(in) Index of the Icon that is to be extracted. Can be set to zero 0 but it needs a reference to return.</param>
    		/// <returns>The handle for the extracted icon.</returns>
    		[DllImport("shell32.DLL", EntryPoint = "ExtractAssociatedIcon")] private static extern int ExtractAssociatedIconA(int hInst,string lpIconPath,ref int lpiIcon);
    		/// <summary>
    		/// Stores the file name that contains the icon.
    		/// </summary>
    		private string fileName;
    		/// <summary>
    		/// Stores the handle of the application requesting the icon.
    		/// </summary>
    		private int iHandle;
    		/// <summary>
    		/// Stores the integral value for the size of the icon that is to be extracted.
    		/// </summary>
    		private int iconSize;
    		/// <summary>
    		/// Stores the boolean value specifing that if the file is an icon library.
    		/// </summary>
    		private bool libFile;
    		/// <summary>
    		/// Integral value representing Large Icons (32x32).
    		/// </summary>
    		public const int LargeIcon = 0;
    		/// <summary>
    		/// Integral value representing Small Icons (16x16).
    		/// </summary>
    		public const int SmallIcon = 1;
    		/// <summary>
    		/// Stores IconHandles to be Destroyed.
    		/// </summary>
    		private System.Collections.ArrayList iconHandles = new System.Collections.ArrayList();
    		
    		/// <summary>
    		/// IconExtractor Contructor - This call will create a new instance of the IconExtractor Object
    		/// </summary>
    		/// <param name="File_Name">The fully qualified file name of the file that is the source of the icon to be extracted.</param>
    		/// <param name="App_Handle">The handle for the application that resquesting the icon.</param>
    		/// <param name="Icon_Size">This should be specified by the LargeIcon (0) or SmallIcon (1) contants.
    		/// If it is specied as something else a small icon (16x16) will be extracted.</param>
    		/// <param name="File_Is_Library">This will tell the IconExtractor object if the file specified is an Icon Library.
    		/// Usually executables(.exe), dynamic link libraries(.dll) and icon files(.ico) are library files.</param>
    		public IconExtractor(string File_Name, int App_Handle, int Icon_Size, bool File_Is_Library)
    		{
    			fileName = @File_Name;
    			iHandle = App_Handle;
    			iconSize = Icon_Size;
    			libFile = File_Is_Library;
    		}
    		
    		/// <summary>
    		/// Sets or gets the file name from which the icon is to be extracted from.
    		/// </summary>
    		public string FileName
    		{
    			get { return fileName; }
    			set { fileName = value; }
    		}
    		
    		/// <summary>
    		/// Sets or gets the handle of the appliaction that is requesting the Icon.
    		/// </summary>
    		public int AppHandle
    		{
    			get { return iHandle; }
    			set { iHandle = value; }
    		}
    		
    		/// <summary>
    		/// Sets or gets the integral value of the Icon Size to be extracted.
    		/// One '1' for small icon (16x16) or zero '0' for large icon (32x32).
    		/// </summary>
    		public int IconSize
    		{
    			get { return iconSize; }
    			set { iconSize = value; }
    		}
    		/// <summary>
    		/// Sets or gets the file type that the icon is to be extracted from.
    		/// </summary>
    		public bool FileIsIconLibrary
    		{
    			get { return libFile; }
    			set { libFile = value; }
    		}
    		
    		/// <summary>
    		/// Method used to extract the icon.
    		/// </summary>
    		/// <returns>Return a System.Drawing.Icon object.</returns>
    		public System.Drawing.Icon GetIcon()
    		{
    			System.Drawing.Icon thisIcon;
    			System.IntPtr thisHandle = new IntPtr();
    			try 
    			{
    				if (libFile == true)
    				{
    					int hLarge = 0;
    					int hSmall = 0;
    					if(ExtractIconExA(fileName, 0,ref hLarge,ref hSmall, 1) > 0)
    					{
    						if(iconSize == LargeIcon)
    						{
    							thisHandle = new IntPtr(hLarge);
    						}
    						else
    						{
    							thisHandle = new IntPtr(hSmall);
    						}
    						iconHandles.Add(hLarge);
    						iconHandles.Add(hSmall);
    					}
    				}
    				else
    				{
    					int RefInt = 0;
    					thisHandle = new IntPtr(ExtractAssociatedIconA(iHandle, fileName, ref RefInt));
    					iconHandles.Add(thisHandle.ToInt32());
    				}
    				thisIcon = System.Drawing.Icon.FromHandle(thisHandle);
    				return (System.Drawing.Icon)thisIcon.Clone();
    			}
    			catch
    			{
    				return null;
    			}
    		}
    		/// <summary>
    		/// Destroys all handles for the icons that were extracted with this class.
    		/// </summary>
    		public void Dispose()
    		{
    			foreach(int i in iconHandles)
    			{
    				if ( i > 0 )
    				{
    					DestroyIcon(i);
    				}
    			}
    		}
    	}
    }


    Caillen
    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    2013年9月12日 12:13
    版主

全部回复

  • 提取文件这个直接使用Directory.GetFiles()方法就可以,要提取对应文件的图片直接调用

    Icon.ExtractAssociatedIcon(file.FullName);方法,具体你要的实现在MSDN已经给出,可以参考下面链接中的实现:

    http://msdn.microsoft.com/zh-cn/library/ms404308.aspx


    If my post is helpful,please help to vote as helpful, if my post solve your question, please help to make it as answer. My sample

    2013年9月11日 9:46
  • hello,

    是这个资源挡吗?

    http://www.dotblogs.com.tw/yc421206/archive/2009/11/04/11398.aspx


    秘訣無它,唯勤而已 http://www.dotblogs.com.tw/yc421206/

    2013年9月12日 10:56
  • 使用shell32.dll里面的ExtractAssociateIconA方法来获取看看,示例代码如下:

    using System;
    using System.Runtime.InteropServices;
    namespace CGetIcon
    {
    	/// <summary>
    	/// This class is used to extract Icons from Executables and file types.
    	/// </summary>
    	/// <remarks>Designed by Daniel Romano</remarks>
    	public class IconExtractor
    	{
    		/// <summary>
    		/// This is the declararion of the DestroyIcon Windows API call from User32.dll
    		/// </summary>
    		/// <param name="hicon">The handle for the icon that will be disposed by the function.</param>
    		/// <returns></returns>
    		[DllImport("User32.DLL")] private static extern int DestroyIcon(int hicon);
    		/// <summary>
    		/// This is the declaration of the ExtractIcon Windows API call from shell32.dll.
    		/// This call can only be used with Library files (.exe, .dll, .ico).
    		/// </summary>
    		/// <param name="lpszFile">(in) The fully qualified file name of the library</param>
    		/// <param name="nIconIndex">(in) The index number position of the icon within the library</param>
    		/// <param name="phiconLarge">(out) This returns the handle of the Large Icon (32x32)</param>
    		/// <param name="phiconSmall">(out) This returns the handle of the Small Icon (16x16)</param>
    		/// <param name="nIcon">(in) The number of Icons to be extracted from the library.</param>
    		/// <returns>The handle for the extracted icons.</returns>
    		[DllImport("shell32.DLL", EntryPoint = "ExtractIconEx")] private static extern int ExtractIconExA(string lpszFile,int nIconIndex,ref int phiconLarge,ref int phiconSmall,int nIcon);
    		/// <summary>
    		/// This is the declaration of the ExtractAssociatedIcon Windows API call from shell32.dll
    		/// This call can only be used with non-library files (eg: .doc | Word Document File)
    		/// </summary>
    		/// <param name="hInst">(in) Handle of the application that is requesting the Icon.</param>
    		/// <param name="lpIconPath">(in) Path to the file that is Associated with the Icon.</param>
    		/// <param name="lpiIcon">(in) Index of the Icon that is to be extracted. Can be set to zero 0 but it needs a reference to return.</param>
    		/// <returns>The handle for the extracted icon.</returns>
    		[DllImport("shell32.DLL", EntryPoint = "ExtractAssociatedIcon")] private static extern int ExtractAssociatedIconA(int hInst,string lpIconPath,ref int lpiIcon);
    		/// <summary>
    		/// Stores the file name that contains the icon.
    		/// </summary>
    		private string fileName;
    		/// <summary>
    		/// Stores the handle of the application requesting the icon.
    		/// </summary>
    		private int iHandle;
    		/// <summary>
    		/// Stores the integral value for the size of the icon that is to be extracted.
    		/// </summary>
    		private int iconSize;
    		/// <summary>
    		/// Stores the boolean value specifing that if the file is an icon library.
    		/// </summary>
    		private bool libFile;
    		/// <summary>
    		/// Integral value representing Large Icons (32x32).
    		/// </summary>
    		public const int LargeIcon = 0;
    		/// <summary>
    		/// Integral value representing Small Icons (16x16).
    		/// </summary>
    		public const int SmallIcon = 1;
    		/// <summary>
    		/// Stores IconHandles to be Destroyed.
    		/// </summary>
    		private System.Collections.ArrayList iconHandles = new System.Collections.ArrayList();
    		
    		/// <summary>
    		/// IconExtractor Contructor - This call will create a new instance of the IconExtractor Object
    		/// </summary>
    		/// <param name="File_Name">The fully qualified file name of the file that is the source of the icon to be extracted.</param>
    		/// <param name="App_Handle">The handle for the application that resquesting the icon.</param>
    		/// <param name="Icon_Size">This should be specified by the LargeIcon (0) or SmallIcon (1) contants.
    		/// If it is specied as something else a small icon (16x16) will be extracted.</param>
    		/// <param name="File_Is_Library">This will tell the IconExtractor object if the file specified is an Icon Library.
    		/// Usually executables(.exe), dynamic link libraries(.dll) and icon files(.ico) are library files.</param>
    		public IconExtractor(string File_Name, int App_Handle, int Icon_Size, bool File_Is_Library)
    		{
    			fileName = @File_Name;
    			iHandle = App_Handle;
    			iconSize = Icon_Size;
    			libFile = File_Is_Library;
    		}
    		
    		/// <summary>
    		/// Sets or gets the file name from which the icon is to be extracted from.
    		/// </summary>
    		public string FileName
    		{
    			get { return fileName; }
    			set { fileName = value; }
    		}
    		
    		/// <summary>
    		/// Sets or gets the handle of the appliaction that is requesting the Icon.
    		/// </summary>
    		public int AppHandle
    		{
    			get { return iHandle; }
    			set { iHandle = value; }
    		}
    		
    		/// <summary>
    		/// Sets or gets the integral value of the Icon Size to be extracted.
    		/// One '1' for small icon (16x16) or zero '0' for large icon (32x32).
    		/// </summary>
    		public int IconSize
    		{
    			get { return iconSize; }
    			set { iconSize = value; }
    		}
    		/// <summary>
    		/// Sets or gets the file type that the icon is to be extracted from.
    		/// </summary>
    		public bool FileIsIconLibrary
    		{
    			get { return libFile; }
    			set { libFile = value; }
    		}
    		
    		/// <summary>
    		/// Method used to extract the icon.
    		/// </summary>
    		/// <returns>Return a System.Drawing.Icon object.</returns>
    		public System.Drawing.Icon GetIcon()
    		{
    			System.Drawing.Icon thisIcon;
    			System.IntPtr thisHandle = new IntPtr();
    			try 
    			{
    				if (libFile == true)
    				{
    					int hLarge = 0;
    					int hSmall = 0;
    					if(ExtractIconExA(fileName, 0,ref hLarge,ref hSmall, 1) > 0)
    					{
    						if(iconSize == LargeIcon)
    						{
    							thisHandle = new IntPtr(hLarge);
    						}
    						else
    						{
    							thisHandle = new IntPtr(hSmall);
    						}
    						iconHandles.Add(hLarge);
    						iconHandles.Add(hSmall);
    					}
    				}
    				else
    				{
    					int RefInt = 0;
    					thisHandle = new IntPtr(ExtractAssociatedIconA(iHandle, fileName, ref RefInt));
    					iconHandles.Add(thisHandle.ToInt32());
    				}
    				thisIcon = System.Drawing.Icon.FromHandle(thisHandle);
    				return (System.Drawing.Icon)thisIcon.Clone();
    			}
    			catch
    			{
    				return null;
    			}
    		}
    		/// <summary>
    		/// Destroys all handles for the icons that were extracted with this class.
    		/// </summary>
    		public void Dispose()
    		{
    			foreach(int i in iconHandles)
    			{
    				if ( i > 0 )
    				{
    					DestroyIcon(i);
    				}
    			}
    		}
    	}
    }


    Caillen
    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    2013年9月12日 12:13
    版主
  • 這應該不是資源管理器吧..

    秘訣無它,唯勤而已 http://www.dotblogs.com.tw/yc421206/

    2013年9月24日 1:40