sharpshell
sharpshell copied to clipboard
PreviewHandler not works for system know file extension(.cs,.txt and so on)
when i using PreviewHandler, i found it can not do preview for system known file extension, for example, .cs, .txt, anyone who have ever encountered this issue? or anyone who know how to fix this issue?
Below image is my code snip,
Hey @akaksohot. Can you first please change the issue-description according to the template for consistency (see the template when creating a new issue)? Also, depending on which version of SharpShell you are using, this could explain the issue.
Please check the registry yourself according to https://github.com/dwmkerr/sharpshell/issues/229#issuecomment-435536487. Maybe the "Class" is missing in the registry, which should be fixed in the most current version of the project.
Also you could try to use AssociationType.FileExtension
and see if that works (even if this is deprecated, which resulted in issues before, see https://github.com/dwmkerr/sharpshell/issues/283).
Hi Countryen, Very thanks for your quick reply. SharpShell functions are very great. My ShareShell is 2.7.2.0, window version is win10. What i want to do is to make all files and directory have my same custom preview window using SharpPreviewHandler. So i set as below, but it does not work for some system extension which windows system have it's own preview content. [COMServerAssociation(AssociationType.AllFilesAndFolders)] [COMServerAssociation(AssociationType.AllFiles)]
Based on above issue, i have tried to add for .cs and .txt, and remove AssociationType.AllFilesAndFolders and AssociationType.AllFiles, as below, it will work for .cs and .txt, but i think many kinds of file have it's own preview content(from windows system), i cannot add for every kind of file. So how do i make every kind of file have my custom preview content by SharpPreviewHandler. [COMServerAssociation(AssociationType.ClassOfExtension, ".cs")] [COMServerAssociation(AssociationType.ClassOfExtension, ".txt")]
Will Attach more image in next comment.
Below is the images.
Below is my code.
using System.Runtime.InteropServices;
using System.Windows.Forms;
using SharpShell.Attributes;
using SharpShell.SharpPreviewHandler;
namespace AbcPreviewHandler
{
/// <summary>
/// The Abc Preview Handler is a preview handler that shows a simple
/// coloured background for ABC files.
/// </summary>
[ComVisible(true)]
//Case 1
//This is work for .cs and .txt, if want to support more kinds of file which windows system have it's prview content, i need to add more ClassOfExtension.
//And I donot know all kinds of these files which windows system have it's prview content
[COMServerAssociation(AssociationType.ClassOfExtension, ".cs")]
[COMServerAssociation(AssociationType.ClassOfExtension, ".txt")]
//Case 2
//Below two line does not work for .cs and .txt which have it's preview content from windows system.
//[COMServerAssociation(AssociationType.AllFilesAndFolders)]
//[COMServerAssociation(AssociationType.AllFiles)]
[DisplayName("Abc Preview Handler")]
[Guid("B86199F1-13D0-4711-AFE6-08C1E4C58905")]
[PreviewHandler(DisableLowILProcessIsolation = false)]
public class AbcPreviewHandler : SharpPreviewHandler
{
/// <summary>
/// DoPreview must create the preview handler user interface and initialize it with data
/// provided by the shell.
/// </summary>
/// <returns>
/// The preview handler user interface.
/// </returns>
protected override PreviewHandlerControl DoPreview()
{
// Create the handler control.
var handler = new AbcPreviewHandlerControl();
MessageBox.Show(SelectedFilePath);
// Do we have a file path? If so, we can do a preview.
if (!string.IsNullOrEmpty(SelectedFilePath))
{
handler.DoPreview(SelectedFilePath);
}
// Return the handler control.
return handler;
}
}
}
[Edit, Countryen, 7. December 2019: Code format]
Hey @akaksohot,
so you want to override the preview for ALL file-extensions that have a certain (system) preview handler?
I don't think it's possible. You need to specify which extensions/classes your handler registers. And, I think, it's by design that a "specific extension" overrites the "All files and folder" ones, by default.
You might wanna do a reg-scan, look for any extension with this specific preview handler, and then register your handler for every type (override).
I'd recommend using a GUI for the user to chose the extensions. Depending on what you want to achieve, the user could want to chose the preview handler per extension and you shouldn't "enforce" yours.
Maybe there is already an easy way to find "all extensions with preview handler X"?
hi Countryen, Thanks for your response. Confirm that if you mean i can not use the Case 2 code above to implement override all files which include system file '.cs' and '.txt' and so on. I have to user Case1 code above to add for each kind of file(inlcude have system preview content and do not have)?
You should be able to combine them, did you try that?
[ComVisible(true)]
[COMServerAssociation(AssociationType.ClassOfExtension, ".cs")]
[COMServerAssociation(AssociationType.ClassOfExtension, ".txt")]
[COMServerAssociation(AssociationType.AllFilesAndFolders)]
[DisplayName("Abc Preview Handler")]
[Guid("B86199F1-13D0-4711-AFE6-08C1E4C58905")]
[PreviewHandler(DisableLowILProcessIsolation = false)]
public class AbcPreviewHandler : SharpPreviewHandler {...}
This should add your handler to * (& Directory & Folder) & .cs & .txt in HKCR.
will try later. But if have more file type have system preview content, i have to add more attribute for these files? I think this is not convenient for me.
will try later. But if have more file type have system preview content, i have to add more attribute for these files? I think this is not convenient for me.
Yes, you have to specify them explicitely, even then they might not work as there is no guarantee that your handler will override the others.
You can also remove the attributes and just write your own Register-/Unregister-Handler, if that is more convenient to you.
As said, I recommend searching the registry for all the extensions/types.
how to search the registry, using what key word or how to search? could you please give some advise on this? Thanks in advance again for your help and response.
There are multiple tutorials/how-to online, here a few:
- https://dotnet-snippets.de/snippet/using-the-registry/216
- https://dotnet-snippets.de/snippet/nuetzliches-aus-der-registry/428
- https://www.infoworld.com/article/3073167/how-to-access-the-windows-registry-using-c.html
Including the MSDN / Microsoft Docs: https://docs.microsoft.com/de-de/dotnet/api/microsoft.win32.registry?view=netframework-4.8
I don't know if there is a "search for X in Y" function, but you could just iterate over all in HKCR and check the keys for yourself. You'd need only to check the ones starting with "." but also the ones with classes for extensions - so maybe better all of them. I think, you also need to check shell and shellex
Will try the combin attributes above and the search registry approach that you approvided later, then will update the result here. Thanks a lot for your help.
Hey @akaksohot have you figured out a way? I'd be happy to read your solution (maybe others, too). Also, please remember to close this issue, if the actual "problem with sharpshell" has been resolved for you :)
I have tried to combin Case 1 and Case 2 attributes, but not works, only Case 1 will works, Case 2 will not work it means case 2 do not have affect on the file preview content, no preview content for the files type except case 1(.cs,.txt).