This repository was archived by the owner on Jan 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathShortcutMenu.cs
More file actions
102 lines (97 loc) · 4.42 KB
/
ShortcutMenu.cs
File metadata and controls
102 lines (97 loc) · 4.42 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
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Reflection;
namespace GoogleImageShell
{
public static class ShortcutMenu
{
private const string ShellKeyPathFormat = @"Software\Classes\SystemFileAssociations\{0}\shell";
private const string VerbName = "GoogleImageShell";
private const string CommandKey = "command";
private static readonly Dictionary<ImageFileType, string[]> FileTypeMap = new Dictionary<ImageFileType, string[]>
{
{ImageFileType.JPG, new[] {".jpg", ".jpe", ".jpeg", ".jfif"}},
{ImageFileType.GIF, new[] {".gif"}},
{ImageFileType.PNG, new[] {".png"}},
{ImageFileType.BMP, new[] {".bmp"}}
};
/// <summary>
/// Creates a shell command to run this program.
/// </summary>
/// <param name="includeFileName">Whether to include the image file name when uploading</param>
/// <param name="resizeOnUpload">Whether to resize large images when uploading</param>
/// <returns>The shell command string</returns>
private static string CreateProgramCommand(bool includeFileName, bool resizeOnUpload)
{
string exePath = new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath;
string command = exePath + " search \"%1\"";
if (includeFileName)
{
command += " -n";
}
if (resizeOnUpload)
{
command += " -r";
}
return command;
}
/// <summary>
/// Opens the shell key corresponding to this program,
/// with read/write permissions.
/// </summary>
/// <param name="allUsers">true if installing for all users, false if for current user</param>
/// <param name="fileType">File extension (".jpg", ".png", etc)</param>
/// <returns>Registry key object for the specified user/file type</returns>
private static RegistryKey GetShellKey(bool allUsers, string fileType)
{
RegistryKey hiveKey = allUsers ? Registry.LocalMachine : Registry.CurrentUser;
string shellPath = string.Format(ShellKeyPathFormat, fileType);
RegistryKey shellKey = hiveKey.CreateSubKey(shellPath);
return shellKey;
}
/// <summary>
/// Adds the program to the Windows Explorer context menu.
/// </summary>
/// <param name="menuText">The text to display on the context menu</param>
/// <param name="includeFileName">Whether to include the image file name when uploading</param>
/// <param name="allUsers">Whether to install for all users</param>
/// <param name="resizeOnUpload">Whether to resize large images when uploading</param>
/// <param name="types">Image file types to install the handler for</param>
public static void InstallHandler(string menuText, bool includeFileName, bool allUsers, bool resizeOnUpload, ImageFileType[] types)
{
string command = CreateProgramCommand(includeFileName, resizeOnUpload);
foreach (ImageFileType fileType in types)
{
foreach (string typeExt in FileTypeMap[fileType])
{
using (RegistryKey shellKey = GetShellKey(allUsers, typeExt))
using (RegistryKey verbKey = shellKey.CreateSubKey(VerbName))
using (RegistryKey cmdKey = verbKey.CreateSubKey(CommandKey))
{
verbKey.SetValue("", menuText);
cmdKey.SetValue("", command);
}
}
}
}
/// <summary>
/// Removes the program from the Windows Explorer context menu.
/// </summary>
/// <param name="allUsers">Whether to uninstall for all users</param>
/// <param name="types">Image file types to uninstall the handler for</param>
public static void UninstallHandler(bool allUsers, ImageFileType[] types)
{
foreach (ImageFileType fileType in types)
{
foreach (string typeExt in FileTypeMap[fileType])
{
using (RegistryKey shellKey = GetShellKey(allUsers, typeExt))
{
shellKey?.DeleteSubKeyTree(VerbName, false);
}
}
}
}
}
}