Skip to content

Commit c745455

Browse files
committed
新增资源管理器右键菜单服务
1 parent ef9562e commit c745455

File tree

3 files changed

+74
-1
lines changed

3 files changed

+74
-1
lines changed

ContextMenuDll/KitopiaExplorerCommand.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public class KitopiaExplorerCommand : Shell32.IExplorerCommand
1515

1616
private void Log(string message)
1717
{
18+
Debug.WriteLine(message);
1819
try
1920
{
2021
var logPath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "KitopiaContextMenu.log");
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using System;
2+
using System.IO;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using PluginCore;
6+
using Windows.Management.Deployment;
7+
8+
namespace Core.Window;
9+
10+
public class ExplorerContextMenuService : IExplorerContextMenuService
11+
{
12+
public async Task<bool> RegisterAsync()
13+
{
14+
try
15+
{
16+
var exePath = AppDomain.CurrentDomain.BaseDirectory;
17+
var manifestPath = Path.Combine(exePath, "ContextMenuDll", "AppxManifest.xml");
18+
19+
if (!File.Exists(manifestPath))
20+
{
21+
manifestPath = Path.Combine(exePath, "AppxManifest.xml");
22+
}
23+
24+
if (!File.Exists(manifestPath))
25+
{
26+
return false;
27+
}
28+
29+
var packageManager = new PackageManager();
30+
var options = new RegisterPackageOptions
31+
{
32+
ExternalLocationUri = new Uri(exePath),
33+
AllowUnsigned = true,
34+
DeveloperMode = true
35+
};
36+
37+
var result = await packageManager.RegisterPackageByUriAsync(new Uri(manifestPath), options);
38+
return result.ExtendedErrorCode == null;
39+
}
40+
catch (Exception)
41+
{
42+
return false;
43+
}
44+
}
45+
46+
public async Task<bool> UnregisterAsync()
47+
{
48+
try
49+
{
50+
var packageManager = new PackageManager();
51+
var packages = packageManager.FindPackagesForUser(string.Empty);
52+
var package = packages.FirstOrDefault(p => p.Id.Name == "Kitopia" && p.Id.Publisher == "CN=Kitopia");
53+
54+
if (package != null)
55+
{
56+
var result = await packageManager.RemovePackageAsync(package.Id.FullName);
57+
return result.ExtendedErrorCode == null;
58+
}
59+
60+
return false;
61+
}
62+
catch (Exception)
63+
{
64+
return false;
65+
}
66+
}
67+
}

KitopiaAvalonia/Program.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ private static IServiceProvider ConfigureServices()
128128
services.AddSingleton<IWindowTool, WindowToolServiceWindow>();
129129
services.AddTransient<IApplicationService, ApplicationService>();
130130
services.AddTransient<IImageTool, ImageTool>();
131+
services.AddTransient<IExplorerContextMenuService, ExplorerContextMenuService>();
131132
#endif
132133

133134
#if LINUX
@@ -236,7 +237,11 @@ public static void OnStartup(string[] arg)
236237
ConfigManger.Init();
237238
Logger.Information("配置文件初始化完成");
238239
if (ConfigManger.Config.mouseCapture) HotKeyManager.HotKetImpl.StartHook();
239-
240+
241+
ServiceManager.Services.GetService<IExplorerContextMenuService>()!.RegisterAsync()
242+
.GetAwaiter()
243+
.GetResult();
244+
Logger.Information("资源管理器右键菜单注册完成");
240245

241246

242247
switch (ConfigManger.Config.themeChoice)

0 commit comments

Comments
 (0)