Ticket Q585034
Visible to All Users

Xtragrid WinExplorerView: How to obtain icon/Image using only the file's extension?

created 11 years ago

Hi,
I've been reviewing the GridMainDemo in the DevExpress Demos 13.2 folder.
In this demo there are two examples: WinExplorerView and WinExplorerViewDatabaseBinding. In the first you obtain a file's icon (or image) using the IconExtractor class by referencing the file's path using this function:

Public Shared Function GetFileIcon(ByVal path As String, ByVal size As IconSize) As Icon Dim shinfo As New SHFILEINFO() Dim retVal As IntPtr = SHGetFileInfo(path, 0, shinfo, CUInt(Marshal.SizeOf(shinfo)), CUInt(Fix(SHGFI_SYSICONINDEX Or SHGFI_ICON))) Dim iconIndex As Integer = shinfo.iIcon Dim iImageList As IImageList = CType(GetSystemImageListHandle(size), IImageList) Dim hIcon As IntPtr = IntPtr.Zero If iImageList IsNot Nothing Then iImageList.GetIcon(iconIndex, CInt(ILD_TRANSPARENT), hIcon) End If Dim icon As Icon = Nothing If hIcon <> IntPtr.Zero Then icon = TryCast(Icon.FromHandle(hIcon).Clone(), Icon) DestroyIcon(shinfo.hIcon) End If Return icon End Function
However, the file has to physically exist on a drive for this to show the correct icon at the chosen size.
In the second example you retrieve an image to display that has already been saved to the database.

In my case I want to use a combination of both of these examples by saving files (documents) to a database. Now while I know the file type (ie extension) I haven't yet found a way to display the correct icon when referencing only the extension (eg .docx). I don't want to save the file's icon/image to the database when the file is originally saved there for obvious reasons. Instead, I save just the file's extension eg .docx into a varchar(5) field and the document's content to a varchar(max).

Using the DX WinExplorerView I want to be able to display to the user the 'file' icons that correspond to the of the type of file whose data is saved in the database. I can do this for size 16x16 and 32x32 icons/images using win32 system calls but I can't display the 48x48 or 256x256 images.

Are you able to provide a code sample that will allow me to achieve this using your IconExtractor class?

Many thanks

Answers approved by DevExpress Support

created 11 years ago (modified 11 years ago)

Hello,
From what I gather, you want to display icons in WinExplorerView by using only file names (and its extensions). To display icons in WinExplorerView, you need to create your own data source instance that contains file names and icons and assign the required columns to the WinExplorerView.ColumnSet.TextColumn and WinExplorerView.ColumnSet.LargeImageColumn properties (see the "Column Set" section in the WinExplorer View help topic for more information).
I've attached a sample illustrating how to attach a grid with the WinExplorerView to a folder. To obtain an icon, I use the Icon.ExtractAssociatedIcon method (see the ConvertToExplorerFile method). This method works for all files but doesn't obtain a folder icon (to test the sample set the "directory" variable to a folder with files only). I've found an approach on how to obtain icons for files and folders in the Obtaining (and managing) file and folder icons using SHGetFileInfo in C# topic.
Feel free to contact us if you need any further assistance.

    Comments (2)

      No, not really. I had already got past the stages that you describe.
      As you know, the WinExplorerView of the gridcontrol provides the gridview.OptionsView.Style property that contains 8 possible values. I wanted to give my clients the option of choosing the Large and ExtraLarge values for the icon/image size but there is no easy way to obtain the file icons (bitmaps) at those sizes without using win32 api calls. The ExtractAssociatedIcon method you mentioned will only fetch 16x16 and 32x32 sized icons. Also, the method only produces an icon for files that physically exist on a local disc ie you need to provide the file's full path.
      I realise now that my original question isn't directly related to the WinExplorerView so I should not have posted it. However, I've resolved my issue and for anyone else who reads this post I attach a class that can be used to obtain icons for any of the sizes in the WinExplorerView. The credit for the solution goes to this post:
      http://stackoverflow.com/questions/17222204/get-default-jumbo-system-icon-based-on-file-extension
      and NOTE you only have to provide the file's extension and not the full file path to retrieve the icon.
      Once again thank you for your input. It would be really worthwhile if your developers could use this class to enhance the functionality of the WinExplorerView
      Regards
      Richard

      Andrew Ser (DevExpress Support) 11 years ago

        Hi,
        I'm happy to hear that you have resolved this issue. Thank you for letting us know of your progress.
        Please feel free to contact us if you have any difficulties. We will be happy to assist you.

        Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

        Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.