How can I get the icon corresponding to a file's type in C#?
I need to get the icons associated with file types not the file name.
I store in my database files with the type=> filename.type, but those files need not exist on the hard disk.
Therefore I need to pass the type and get the icon for it
- I do not want to use solutions with administrative rights needed by the user.
- If possibl开发者_如何学Ce only managed code
- If possible don't involve registry access
What do you suggest?
You can call SHGetFileInfo()
.
If the file does not exist then you will need to use the SHGFI_USEFILEATTRIBUTES
flag:
If the uFlags parameter includes the SHGFI_USEFILEATTRIBUTES flag, this parameter does not have to be a valid file name. The function will proceed as if the file exists with the specified name and with the file attributes passed in the dwFileAttributes parameter. This allows you to obtain information about a file type by passing just the extension for pszPath and passing FILE_ATTRIBUTE_NORMAL in dwFileAttributes.
As usual, pinvoke.net has the necessary information to get to this from managed code.
You can use the Win32 API SHGetFileInfo, but I think the file should exist for it to work.
精彩评论