How to get the associated icon from a network share file

后端 未结 5 1439
深忆病人
深忆病人 2020-12-07 00:21

I am using Icon.ExtractAssociatedIcon to get the icon of a file , that a user selects, in an openfiledialog.

THe problem is if the user selects an icon from a netw

5条回答
  •  南方客
    南方客 (楼主)
    2020-12-07 00:56

    Looking at this with Reflector, it is ultimately calling ExtractAssociatedIcon in shell32.dll.

    Have you tried the going around the BCL accessing it via PInvoke?

    Sample code (via PInvoke.Net):

    [DllImport("shell32.dll", CharSet = CharSet.Unicode)]
    static extern IntPtr ExtractAssociatedIcon(IntPtr hInst, StringBuilder lpIconPath,
       out ushort lpiIcon);
    
     // ... snip
        ushort uicon;
        StringBuilder strB = new StringBuilder(260); // Allocate MAX_PATH chars
        strB.Append(openFileDialog1.FileName);
        IntPtr handle = ExtractAssociatedIcon(IntPtr.Zero, strB, out uicon);
        Icon ico = Icon.FromHandle(handle);
    
        pictureBox1.Image = ico.ToBitmap();
     // ... snip
    

提交回复
热议问题