Click here to Skip to main content
15,120,326 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I’ve been having some troubles implementing file preview control utilizing the existing preview handlers (not looking into writing custom preview handlers). I could not get it to work for ".txt", ".jpg", ".bmp", ".dwg", ".mp3".

Here are my findings:

No.1
As most posts mention the preview handler CLSID in registry is not always set for an extension. It could be specified under class name. However what I found that it could be also specified under Perceived Type. That is a case for “.txt” files. I modified the code to look handler CLSID under perceived type if can’t find under extension or class name. After this change the preview works for all “text” files (.txt, .sql, .vb, .cs etc.)

No. 2
I found “mp3file” class name that has a handler. For testing purposes I hardcoded to search for “mp3file” class name instead of one I got from key “.mp3” and that worked just like in Windows Explorer. Again it seems to be just a matter of getting CLSID. After more testing I figured there was another issue. I had VLC player, that changed “.mp3” class name to “VLC.pm3” and that one didn’t have handler’s ID. I looked up “.mp3” on machine that didn’t have VLC, it had a class name “WMP11.AssocFile.MP3” – this one handler’s ID (same as mp3file). After uninstalling VLC on my machine, “.mp3” class name reverted back to “WMP11.AssocFile.MP3” and the preview started working. Note: while still having VLC installed resetting a default program, changing file associations from VLC to Windows Media Player did not change the “.mp3” class name, so I was still unable to get CLSID. But the strange thing is that Windows Explorer showed the preview at all times. It somehow still knew how to get the correct CLSID for “.mp3”. Could it be getting it always from “mp3file” regardless of what class name is set for “.mp3”?

No. 3
I have looked all over the registry and cannot find any handler registrations for any of images. Then I reviewed the list of available handlers under
HKEY_LOCAL_MACHINE or HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\PreviewHandlers
I don’t see one for images. Does Windows Explorer handle images differently, not via preview handlers?

So my questions:
1. Am I missing something obvious when getting handler CLSID? What would be a “proper “ way to get CLSID for any file?

2. Handling images is my biggest concern right now. If anyone got the preview working for images and can provide some guidelines it would be much appreciated.

Note: I considered custom handling the images showing them in a PictureBox, but I don’t really like that. First I don’t like inconsistencies. I’d rather use one approach for all files. Secondly my quick test already showed some noticeable differences.

Here’s my current code to get CLSID:
VB
Private Function GetPreviewHandlerGUID(ByVal file As String) As Guid

            Dim previewHandlerGUID As Guid = Guid.Empty

            Try


				Dim key As String = System.IO.Path.GetExtension(file)
				'	If System.IO.Path.GetExtension(file) = ".mp3" Then key = "mp3file"


				'Open the registry key corresponding to the file extension.
				Using extensionKey As RegistryKey = My.Computer.Registry.ClassesRoot.OpenSubKey(key) 

					If extensionKey IsNot Nothing Then

						'Open the key that indicates the GUID of the preview handler type.
						Using previewHandlerGUIDKey As RegistryKey = extensionKey.OpenSubKey("ShellEx\{8895b1c6-b41f-4c1c-a562-0d564250836f}")

							If previewHandlerGUIDKey IsNot Nothing Then
								'Found the key for the extension, get GUID.
								previewHandlerGUID = New Guid(Convert.ToString(previewHandlerGUIDKey.GetValue(Nothing)))

							Else
								'Did not find the key for extension. Check other options.

								'Sometimes preview handlers are declared on key for the class.
								Dim className As String = Convert.ToString(extensionKey.GetValue(Nothing))

								If className IsNot Nothing Then

									Using classKey As RegistryKey = Registry.ClassesRoot.OpenSubKey(className + "\ShellEx\{8895b1c6-b41f-4c1c-a562-0d564250836f}")

										If classKey IsNot Nothing Then
											'Found the key for the class, get GUID.
											previewHandlerGUID = New Guid(Convert.ToString(classKey.GetValue(Nothing)))

										Else
											'Did not find the key for the class. Check other options.

											'Sometimes preview handlers are declared on the key for the perceived type.

											'Get the perceived type key.
											Dim perceivedType As String = Convert.ToString(extensionKey.GetValue("PerceivedType", Nothing))

											If perceivedType IsNot Nothing Then

												Using perceivedTypeKey As RegistryKey = Registry.ClassesRoot.OpenSubKey("SystemFileAssociations\" + perceivedType + "\ShellEx\{8895b1c6-b41f-4c1c-a562-0d564250836f}")

													If perceivedTypeKey IsNot Nothing Then previewHandlerGUID = New Guid(Convert.ToString(perceivedTypeKey.GetValue(Nothing)))

												End Using

											End If

										End If

									End Using

								End If

							End If

						End Using




					End If

				End Using

			Catch ex As Exception
				MsgBox(ex.Message)

			End Try


            Return previewHandlerGUID

        End Function


There are some articles on this topic. Below are a few links, but none seems to be working for all files:
http://www.brad-smith.info/blog/archives/183
https://social.msdn.microsoft.com/forums/vstudio/en-US/59e12744-558b-457b-93bb-048b9e0b027e/file-preview-control-in-c (this one also have links to other including to articles on codeproject)

Thank you.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900