c# - Which Icon corresponds to a tentative filename? -


i have file on binary in database, i'm not loading until user double clicks on item (with it's icon)

extractassociatedicon should solving (i've seen here: extractassociatedicon returns null)

but asks for existance of document on path, (which don't have), how can associate extension of tentative file (example: "something.docx") extension user has associated on pc icon should show?

@edit: may able pull out content (byte[]) database, if useful icon asociated image. (however, may take forever transfer each file byte[] set icon image).

also this example of @matthewwatson using takes 8 seconds on each object value = rkfileicon.getvalue(""); line, has 8.4k items iterate.

@@edit: tried

    public static icon geticonoldschool(string filename)     {         ushort uicon;         stringbuilder strb = new stringbuilder(filename);         intptr handle = extractassociatedicon(intptr.zero, strb, out uicon);         icon ico = icon.fromhandle(handle);          return ico;     } 

with no success.

@@@edit: 1 above gives me icon of file.(and not docx icon when gets .docx)

    [structlayout(layoutkind.sequential)]     public struct shfileinfo     {         public intptr hicon;         public intptr iicon;         public uint dwattributes;         [marshalas(unmanagedtype.byvaltstr, sizeconst = 260)]         public string szdisplayname;         [marshalas(unmanagedtype.byvaltstr, sizeconst = 80)]         public string sztypename;     };     static shfileinfo shinfo = new shfileinfo();     class win32     {         public const uint shgfi_icon = 0x100;         public const uint shgfi_largeicon = 0x0; // 'large icon         public const uint shgfi_smallicon = 0x1; // 'small icon          [dllimport("shell32.dll")]         public static extern intptr shgetfileinfo(string pszpath, uint dwfileattributes, ref shfileinfo psfi, uint cbsizefileinfo, uint uflags);     }     public static icon yetanotherattempt(string fname)     {          //use small icon         var himgsmall = win32.shgetfileinfo(fname, 0, ref shinfo, (uint)marshal.sizeof(shinfo), win32.shgfi_icon | win32.shgfi_smallicon);          //use large icon         //himglarge = shgetfileinfo(fname, 0,          //  ref shinfo, (uint)marshal.sizeof(shinfo),          //  win32.shgfi_icon | win32.shgfi_largeicon);          //the icon returned in hicon member of shinfo struct         return system.drawing.icon.fromhandle(shinfo.hicon);     } 

@@@@edit: results of @alexk. solution.

enter image description here

the filetoimage converter i'm doing magic.

     <listbox.itemtemplate>             <datatemplate>                 <grid>                     <grid.columndefinitions>                         <columndefinition />                         <columndefinition />                     </grid.columndefinitions>                     <stackpanel orientation="horizontal">                         <image source="{binding converter={staticresource filetoimage}}" />                         <textblock text="{binding filename}"                                margin="5,0,0,0"                                verticalalignment="center"/>                     </stackpanel> 

to win32 class add:

public const uint shgfi_usefileattributes = 0x000000010; // better private enum 

and

[dllimport("user32.dll", setlasterror = true)] [return: marshalas(unmanagedtype.bool)] internal static extern bool destroyicon(intptr hicon); 

your call becomes:

var himgsmall = win32.shgetfileinfo(fname, 0, ref shinfo, (uint)marshal.sizeof(shinfo),                  win32.shgfi_icon | win32.shgfi_smallicon | win32.shgfi_usefileattributes); 

to return icon should copy destroy 1 api returned:

var icon = (icon)icon.fromhandle(shinfo.hicon).clone();     win32.destroyicon(shinfo.hicon); return icon; 

example (windowsform proj)

using system; using system.drawing; using system.runtime.interopservices; using system.windows.forms;  namespace windowsformsapplication1 {     public partial class form1 : form     {         public form1() {             initializecomponent();         }          private void form1_load(object sender, eventargs e) {             var shinfo = new win32.shfileinfo();             win32.shgetfileinfo("0000000000.docx", 0, ref shinfo, (uint)marshal.sizeof(shinfo), win32.shgfi_icon | win32.shgfi_smallicon | win32.shgfi_usefileattributes);             var icon = (icon)icon.fromhandle(shinfo.hicon).clone();             win32.destroyicon(shinfo.hicon);             this.backgroundimage = icon.tobitmap();             icon.dispose();         }          private class win32 {             internal const uint shgfi_icon = 0x100;             internal const uint shgfi_smallicon = 0x1;             internal const uint shgfi_usefileattributes = 0x000000010;              [dllimport("user32.dll", setlasterror = true)]             [return: marshalas(unmanagedtype.bool)]             internal static extern bool destroyicon(intptr hicon);              [dllimport("shell32.dll")]             internal static extern intptr shgetfileinfo(string pszpath, uint dwfileattributes, ref shfileinfo psfi, uint cbsizefileinfo, uint uflags);              [structlayout(layoutkind.sequential)]             internal struct shfileinfo             {                 public intptr hicon;                 public intptr iicon;                 public uint dwattributes;                  [marshalas(unmanagedtype.byvaltstr, sizeconst = 260)]                 public string szdisplayname;                  [marshalas(unmanagedtype.byvaltstr, sizeconst = 80)]                 public string sztypename;             }         }     } } 

Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -