Read the file icon by ExtractIconEx

Reference ( https://blog.csdn.net/bruce135lee/article/details/79790104 ), using ExtractIconEx get file / extension icon, in actual use, read the icon will appear stuck situation;

It has been tested and found not appear stuck in a small file directory, but in reading the c: \ windows \ system32 occur when stuck;

It turned MSDN, prompted by the need to free up resources DestroyIcon.

Relative to the reference, the following modifications:

1, add the parameter: tcType need to get used to specify a file or directory; (previously acquired by a problem with the way the file extension, if the file itself is not extension, would be mistaken directory)

2, ExtractIconEx handle list declaration IntPtr type used, to facilitate the subsequent determination;

3, after obtaining the icon resource, call DestroyIcon release resources;

4, read the registry information, data key part of the file name might use quotation marks (such as: "\" C: \ Program Files \ 7-Zip \ 7z.rar \ ", 1") which led to Get Picture failed; thus: the array into a string, trailing spaces removed

5, an icon file directly read, limited only .EXE and .ICO only read the file that comes with the icon; reason: some DLL also with an icon resource will result in DLL files extracted to bring their own icon

The final code:

  . 1          ///  <Summary> 
  2          /// Get the name of the file by the file icon
   . 3          ///  </ Summary> 
  . 4          ///  <param name = "tcType"> tcFullName specified parameter types: the FILE / the DIR </ param> 
  5          ///  <param name = "tcFullName"> need to get the picture file name and path </ param> 
  6          ///  <param name = "tlIsLarge"> whether to acquire large icon (32 * 32) </ param> 
  7          ///  <Returns> </ Returns> 
  . 8          Private Icon GetIconByFileName ( String tcType, String tcFullName,bool tlIsLarge = false)
  9         {
 10             Icon ico = null;
 11 
 12             string fileType = tcFullName.Contains(".") ? tcFullName.Substring(tcFullName.LastIndexOf('.')).ToLower() : string.Empty;
 13 
 14             RegistryKey regVersion = null;
 15             string regFileType = null;
 16             string regIconString = null;
 17             string systemDirectory = Environment.SystemDirectory + "\\" ;
 18 is              IntPtr [] = phiconLarge new new IntPtr [ . 1 ];
 . 19              IntPtr [] = phiconSmall new new IntPtr [ . 1 ];
 20 is              IntPtr the hIcon = IntPtr.Zero;
 21 is              uint RST = 0 ;
 22 is  
23 is              IF (tcType == " the FILE " )
 24              {
 25                  // containing an icon file, comes to use the file icon 
26 is                  IF ( " .exe.ico " .Contains (fileType))
 27                 {
 28                     //文件名 图标索引
 29                     phiconLarge[0] = phiconSmall[0] = IntPtr.Zero;
 30                     rst = ExtractIconEx(tcFullName, 0, phiconLarge, phiconSmall, 1);
 31                     hIcon = tlIsLarge ? phiconLarge[0] : phiconSmall[0];
 32                     ico = hIcon == IntPtr.Zero ? null : Icon.FromHandle(hIcon).Clone() as Icon;
 33                     if (phiconLarge[0!] = IntPtr.Zero) DestroyIcon (phiconLarge [ 0 ]);
 34 is                      IF (phiconSmall [ 0 !] = IntPtr.Zero) DestroyIcon (phiconSmall [ 0 ]);
 35                      IF ! (ICO = null )
 36                      {
 37 [                          return ICO;
 38                      }
 39                  }
 40  
41 is                  // read the file extension icon 
42 is                  regVersion = Registry.ClassesRoot.OpenSubKey (fileType, to false );
 43 is                  IF (! regVersion = null )
 44 is                 {
 45                      regFileType = regVersion.GetValue ( "" ) as  string ;
46                      regVersion.Close ();
47                      regVersion = Registry.ClassesRoot.OpenSubKey (regFileType + @ " \ Default Icon " , false );
48                      if (regVersion! = Null )
 49                      {
 50                          regIconString = regVersion.GetValue ( "" ) as  string ;
51                          regVersion.Close ();
52 is                      }
 53 is                  }
 54 is                  IF (regIconString == null )
 55                  {
 56 is                      // not read the registration information to the file type, the file type is specified as an unknown icon   
57 is                      regIconString = SystemDirectory + " Shell32.dll, 0 " ;
 58                  }
 59              }
 60              the else 
61 is              {
 62 is                  // specified directly as a folder icon   
63 is                  = SystemDirectory + regIconString " Shell32.dll,. 3 " ;
 64             }
 65  
66              String [] = regIconString.Split the fileIcon ( new new  char [] { ' , ' });
 67              // system plot registered in the registry can not be directly extracted, the executable file is generic icon returns   
68              the fileIcon = the fileIcon == .length 2 ? the fileIcon: new new  String [] {SystemDirectory + " Shell32.dll " , " 2 " };
 69              
70              phiconLarge [ 0 ] = phiconSmall [ 0 ] = IntPtr.Zero;
 71 is             rst = ExtractIconEx(fileIcon[0].Trim('\"'), Int32.Parse(fileIcon[1]), phiconLarge, phiconSmall, 1);
 72             hIcon = tlIsLarge ? phiconLarge[0] : phiconSmall[0];
 73             ico = hIcon == IntPtr.Zero ? null : Icon.FromHandle(hIcon).Clone() as Icon;
 74             if (phiconLarge[0] != IntPtr.Zero) DestroyIcon(phiconLarge[0]);
 75             if (phiconSmall[0] != IntPtr.Zero) DestroyIcon(phiconSmall[0 ]);
 76              IF (ICO =! Null )
 77              {
 78                  return ICO;
 79              }
 80  
81              // For file, if the extracted file icon fails, the executable file is reused generic icons 
82              IF (tcType == " the FILE " )
 83              {
 84                  // system plot registered in the registry can not be directly extracted, the executable file is returned generic icon   
85                  the fileIcon = new new  String [] {SystemDirectory + " Shell32.dll " , " 2 " }; 
 86                 phiconLarge = new IntPtr[1];
 87                 phiconSmall = new IntPtr[1];
 88                 rst = ExtractIconEx(fileIcon[0], Int32.Parse(fileIcon[1]), phiconLarge, phiconSmall, 1);
 89                 hIcon = tlIsLarge ? phiconLarge[0] : phiconSmall[0];
 90                 ico = hIcon == IntPtr.Zero ? null : Icon.FromHandle(hIcon).Clone() as Icon;
 91                 if (phiconLarge[0] != IntPtr.Zero) DestroyIcon(phiconLarge[0]);
 92                 if (phiconSmall[0] != IntPtr.Zero) DestroyIcon(phiconSmall[0]);
 93             }
 94 
 95             return ico;
 96         }
 97 
 98         [DllImport("shell32.dll", SetLastError = true)]
 99         private static extern uint ExtractIconEx(string lpszFile, int nIconIndex, IntPtr[] phiconLarge, IntPtr[] phiconSmall, uint nIcons);
100         [DllImport("User32.dll", SetLastError = true)]
101         private static extern uint DestroyIcon(IntPtr phicon);
View Code

 

Guess you like

Origin www.cnblogs.com/xlsoftware/p/11590607.html