{"id":96075,"date":"2017-05-01T07:00:00","date_gmt":"2017-05-01T21:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/?p=96075"},"modified":"2019-03-13T01:10:50","modified_gmt":"2019-03-13T08:10:50","slug":"20170501-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20170501-00\/?p=96075","title":{"rendered":"Why doesn&#8217;t SHGetFileInfo give me customized folder icons?"},"content":{"rendered":"<p>A customer reported that they were unable to obtain cutomized folder icons. Whenever they asked for the icon for a folder, they always got a plain folder icon, Even if they asked for folders like My Documents, which  come with their own custom icons. The customer says that they are using the code from <a HREF=\"https:\/\/www.codeproject.com\/articles\/2532\/obtaining-and-managing-file-and-folder-icons-using\">this Web site<\/a>: <\/p>\n<pre>\npublic static System.Drawing.Icon GetFileIcon(string name, IconSize size, \n                                              bool linkOverlay)\n{\n    Shell32.SHFILEINFO shfi = new Shell32.SHFILEINFO();\n    uint flags = Shell32.SHGFI_ICON | Shell32.SHGFI_USEFILEATTRIBUTES;\n    \n    if (true == linkOverlay) flags += Shell32.SHGFI_LINKOVERLAY;\n\n\n    \/* Check the size specified for return. *\/\n    if (IconSize.Small == size)\n    {\n        flags += Shell32.SHGFI_SMALLICON ; \/\/ include the small icon flag\n    } \n    else \n    {\n        flags += Shell32.SHGFI_LARGEICON ;  \/\/ include the large icon flag\n    }\n    \n    Shell32.SHGetFileInfo( name, \n        Shell32.FILE_ATTRIBUTE_NORMAL, \n        ref shfi, \n        (uint) System.Runtime.InteropServices.Marshal.SizeOf(shfi), \n        flags );\n\n\n    \/\/ Copy (clone) the returned icon to a new object, thus allowing us \n    \/\/ to call DestroyIcon immediately\n    System.Drawing.Icon icon = (System.Drawing.Icon)\n                         System.Drawing.Icon.FromHandle(shfi.hIcon).Clone();\n    User32.DestroyIcon( shfi.hIcon ); \/\/ Cleanup\n    return icon;\n}\n<\/pre>\n<p>As I noted some time ago, <a HREF=\"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/20040601-00\/?p=39073\">the <code>SHGFI_USE&shy;FILE&shy;ATTRIBUTES<\/code> flag means<\/a> &#8220;Pretend that the file\/directory exists, and that its file attributes are what I passed as the dwFileAttributes parameter. Do this regardless of whether it actually exists or not.&#8221; <\/p>\n<p>Since you&#8217;re passing the flag, and you say &#8220;Trust me, it&#8217;s a directory,&#8221; the shell says, &#8220;Okay, then here&#8217;s the icon for a standard directory. I can&#8217;t give you anything better than that because you told me not to access the disk.&#8221; <\/p>\n<p>If you want to get the icon for a live file or directory, then remove the <code>SHGFI_USE&shy;FILE&shy;ATTRIBUTES<\/code> flag. <\/p>\n<p><b>Bonus chatter<\/b>: Yes, somebody posted a comment on that Web site asking how to get the function to work for special folders, and somebody else posted the same answer (namely, remove the <code>SHGFI_USE&shy;FILE&shy;ATTRIBUTES<\/code> flag). But that question\/answer was posted long after our customer asked the question. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Because you asked for it that way.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-96075","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Because you asked for it that way.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/96075","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=96075"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/96075\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=96075"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=96075"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=96075"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}