Repository navigation
Expand file tree
/
Copy pathShellIntegrationHelper.cs
More file actions
126 lines (109 loc) · 3.58 KB
/
Copy pathShellIntegrationHelper.cs
File metadata and controls
126 lines (109 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
using System;
using System.IO;
using System.Runtime.InteropServices;
using Microsoft.Win32;
namespace LinView
{
/// <summary>
/// 文件夹右键菜单集成(HKCU,无需管理员权限)。
/// </summary>
public static class ShellIntegrationHelper
{
/// <summary>注册表项名称(内部标识,不显示给用户)</summary>
private const string MenuKeyName = "LinViewMasonry";
/// <summary>菜单显示文字</summary>
private const string MenuText = "用 LinView 瀑布流打开";
/// <summary>目标类:文件夹 + 驱动器</summary>
private static readonly string[] TargetClasses = { "Directory", "Drive" };
private static string GetKeyPath(string cls)
=> $@"Software\Classes\{cls}\shell\{MenuKeyName}";
public static bool IsInstalled()
{
try
{
foreach (var cls in TargetClasses)
{
using var key = Registry.CurrentUser.OpenSubKey(GetKeyPath(cls));
if (key == null) return false;
}
return true;
}
catch
{
return false;
}
}
public static void Install()
{
try
{
string exePath = System.Windows.Forms.Application.ExecutablePath;
foreach (var cls in TargetClasses)
{
using var key = Registry.CurrentUser.CreateSubKey(GetKeyPath(cls));
if (key == null) continue;
key.SetValue("", MenuText);
key.SetValue("Icon", $"\"{exePath}\",0");
using var cmdKey = key.CreateSubKey("command");
cmdKey?.SetValue("", $"\"{exePath}\" --masonry \"%1\"");
}
NotifyShellChanged();
}
catch (Exception ex)
{
LogHelper.Write(ex, "ShellIntegrationHelper.Install");
}
}
public static void Uninstall()
{
try
{
foreach (var cls in TargetClasses)
{
try
{
Registry.CurrentUser.DeleteSubKeyTree(
GetKeyPath(cls), throwOnMissingSubKey: false);
}
catch { }
}
NotifyShellChanged();
}
catch (Exception ex)
{
LogHelper.Write(ex, "ShellIntegrationHelper.Uninstall");
}
}
public static void Sync(bool enabled)
{
try
{
bool installed = IsInstalled();
if (enabled)
{
Install();
}
else
{
if (installed) Uninstall();
}
}
catch (Exception ex)
{
LogHelper.Write(ex, "ShellIntegrationHelper.Sync");
}
}
private const int SHCNE_ASSOCCHANGED = 0x08000000;
private const uint SHCNF_IDLIST = 0x0000;
[DllImport("shell32.dll")]
private static extern void SHChangeNotify(int wEventId, uint uFlags, IntPtr dwItem1, IntPtr dwItem2);
private static void NotifyShellChanged()
{
try
{
SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, IntPtr.Zero, IntPtr.Zero);
}
catch { }
}
}
}