Automatically fix Office preview handler registry

This function requires QuickLook to run with administrator privileges and preview the corresponding Office file once
This commit is contained in:
ema
2025-06-12 15:25:18 +08:00
parent 9a3591e6f6
commit e2e5885fd5
4 changed files with 103 additions and 35 deletions

View File

@@ -21,7 +21,6 @@ using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Microsoft.Win32;
namespace QuickLook.Plugin.OfficeViewer;
@@ -70,34 +69,6 @@ public class PreviewHandlerHost : Control
base.Dispose(disposing);
}
/// <summary>
/// Returns the GUID of the preview handler associated with the specified file.
/// </summary>
/// <param name="filename"></param>
/// <returns></returns>
public static Guid GetPreviewHandlerGUID(string filename)
{
// open the registry key corresponding to the file extension
var ext = Registry.ClassesRoot.OpenSubKey(Path.GetExtension(filename));
if (ext != null)
{
// open the key that indicates the GUID of the preview handler type
var test = ext.OpenSubKey(@"shellex\{8895b1c6-b41f-4c1c-a562-0d564250836f}");
if (test != null) return new Guid(Convert.ToString(test.GetValue(null)));
// sometimes preview handlers are declared on key for the class
var className = Convert.ToString(ext.GetValue(null));
if (className != null)
{
test = Registry.ClassesRoot.OpenSubKey(
className + @"\shellex\{8895b1c6-b41f-4c1c-a562-0d564250836f}");
if (test != null) return new Guid(Convert.ToString(test.GetValue(null)));
}
}
return Guid.Empty;
}
/// <summary>
/// Resizes the hosted preview handler when this PreviewHandlerHost is resized.
/// </summary>
@@ -123,7 +94,7 @@ public class PreviewHandlerHost : Control
return false;
// try to get GUID for the preview handler
var guid = GetPreviewHandlerGUID(path);
var guid = ShellExRegister.GetPreviewHandlerGUID(Path.GetExtension(path));
if (guid == Guid.Empty)
return false;