Get the icon for a given extension
Use the GetFileIcon
method from this CodeProject article from Paul Ingles and pass .ext
as the name
parameter.
The GetFileIcon
method is a wrapper around the native SHGetFileInfo
and copied here for illustration:
public static System.Drawing.Icon GetFileIcon(string name, IconSize size,
bool linkOverlay)
{
Shell32.SHFILEINFO shfi = new Shell32.SHFILEINFO();
uint flags = Shell32.SHGFI_ICON | Shell32.SHGFI_USEFILEATTRIBUTES;
if (true == linkOverlay) flags += Shell32.SHGFI_LINKOVERLAY;
/* Check the size specified for return. */
if (IconSize.Small == size)
{
flags += Shell32.SHGFI_SMALLICON ; // include the small icon flag
}
else
{
flags += Shell32.SHGFI_LARGEICON ; // include the large icon flag
}
Shell32.SHGetFileInfo( name,
Shell32.FILE_ATTRIBUTE_NORMAL,
ref shfi,
(uint) System.Runtime.InteropServices.Marshal.SizeOf(shfi),
flags );
// Copy (clone) the returned icon to a new object, thus allowing us
// to call DestroyIcon immediately
System.Drawing.Icon icon = (System.Drawing.Icon)
System.Drawing.Icon.FromHandle(shfi.hIcon).Clone();
User32.DestroyIcon( shfi.hIcon ); // Cleanup
return icon;
}
How to get the icon for a file extension or filetype without creating a temp file?
Looks like you already discovered the way to do it, unless you want to dive into native libraries etc.
FileSystemView uses Win32ShellFolder internally so they are basically the same.
I also dug up the Source for org.eclipse.swt.program.Program and with it org.eclipse.swt.internal.win32.OS. The OS class then uses a native call for the Icon. At this point unless you really really cannot create a Temp File i would not go down that path.
For JDIC i only found http://kickjava.com/src/org/jdesktop/jdic/tray/internal/impl/WinTrayIconService.java.htm with a little bit of digging(may not be related but does icony things :D). Also calls native.
Fastest way to get the extension name and icon by an file extension?
Firstly: Are you getting millions of icons? If you're ony getting one or a few hundred, then does it really matter which is fastest? How slow is too slow? THe best way is to try the code and time it to see if it's worth worrying about.
Secondly: Is the speed difference really a problem? Using SHGetFileInfo is more likely to work on every operating system version and will be compatible with however Microsoft do things in future - Reading the registry may not work in some cases. How will you test it?
Finally, having weighed up the above questions, the best approach is to write the 10 lines of code that it will take to try all three approaches and simply see which one is fastest.
How can I get large icons for a file extension using Windows shell?
The sizes for shell icons are given in the documentation for SHGetImageList
:
- 16x16 = small
- 32x32 = large
- 48x48 = extralarge
- 256x256 = jumbo
So it is expected that when you ask for the "large" icons, you get 32x32. If you want the extralarge icon, you need to get them from the SHIL_EXTRALARGE
image list.
How do I get common file type icons in C#?
Take a look at: http://mvolo.com/display-pretty-file-icons-in-your-aspnet-applications-with-iconhandler/
It's not the cleanest solution but it works. Otherwise, try to get your hands on a library of Icons that's based on mime type or file extension.
Related Topics
Request Exceeds the Configured Maxquerystringlength When Using [Authorize]
Calling a C# Library from Python
How to Reference Embedded Images from CSS
Httplistener with Https on Monotouch
How to Add Js and CSS Files in ASP.NET Core
How to Do Polymorphism in Ruby
Load CSS Dynamically in ASP.NET
What Is an Algorithm to Diff the Two Strings in the Same Way That So Does on the Version Page
How to Display Formatted Code in Webpage
Creating a "Hello World" Websocket Example
How to Use CSS on an HTML.Actionlink in C#
How to Convert a C# List<String[]> to a JavaScript Array
Printing to a Client Printer from a Web App
Unitywebrequest Embedding User + Password Data for Http Basic Authentication Not Working on Android
How to Redirecttoaction in ASP.NET MVC Without Losing Request Data