-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShortcutHelper.cs
143 lines (130 loc) · 5.2 KB
/
ShortcutHelper.cs
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
internal static class ShortcutHelper {
/// <summary>
/// windows styles
/// </summary>
public enum ShortcutWindowStyles {
/// <summary>
/// Hide
/// </summary>
WshHide = 0,
/// <summary>
/// NormalFocus
/// </summary>
WshNormalFocus = 1,
/// <summary>
/// MinimizedFocus
/// </summary>
WshMinimizedFocus = 2,
/// <summary>
/// MaximizedFocus
/// </summary>
WshMaximizedFocus = 3,
/// <summary>
/// NormalNoFocus
/// </summary>
WshNormalNoFocus = 4,
/// <summary>
/// MinimizedNoFocus
/// </summary>
WshMinimizedNoFocus = 6
}
/// <summary>
/// Create shortcut in current FilePath.
/// </summary>
/// <param name="linkFileName">shortcut name(include .lnk extension.)</param>
/// <param name="targetPath">target FilePath</param>
/// <param name="workingDirectory">working FilePath</param>
/// <param name="arguments">arguments</param>
/// <param name="hotkey">hot key(ex: Ctrl+Shift+Alt+A)</param>
/// <param name="shortcutWindowStyle">window style</param>
/// <param name="description">shortcut description</param>
/// <param name="iconNumber">icon index(start of 0)</param>
/// <returns>shortcut file FilePath.</returns>
/// <exception cref="FileNotFoundException"></exception>
public static string CreateShortcut(
string linkFileName,
string targetPath,
string workingDirectory = "",
string arguments = "",
string hotkey = "",
ShortcutWindowStyles shortcutWindowStyle = ShortcutWindowStyles.WshNormalFocus,
string description = "",
int iconNumber = 0) {
if (linkFileName.Contains(DefaultShortcutExtension) == false) {
linkFileName = $"{linkFileName}{DefaultShortcutExtension}";
}
if (!File.Exists(targetPath)) {
throw new FileNotFoundException(targetPath);
}
if (workingDirectory == string.Empty) {
workingDirectory = Path.GetDirectoryName(targetPath);
}
string iconLocation = $"{targetPath},{iconNumber}";
if (Environment.Version.Major >= 4) {
Type shellType = Type.GetTypeFromProgID(WscriptShellName);
dynamic shell = Activator.CreateInstance(shellType);
dynamic shortcut = shell.CreateShortcut(linkFileName);
shortcut.TargetPath = targetPath;
shortcut.WorkingDirectory = workingDirectory;
shortcut.Arguments = arguments;
shortcut.Hotkey = hotkey;
shortcut.WindowStyle = shortcutWindowStyle;
shortcut.Description = description;
shortcut.IconLocation = iconLocation;
shortcut.Save();
} else {
Type shellType = Type.GetTypeFromProgID(WscriptShellName);
object shell = Activator.CreateInstance(shellType);
object shortcut = shellType.InvokeMethod("CreateShortcut", shell, linkFileName);
Type shortcutType = shortcut.GetType();
shortcutType.InvokeSetMember("TargetPath", shortcut, targetPath);
shortcutType.InvokeSetMember("WorkingDirectory", shortcut, workingDirectory);
shortcutType.InvokeSetMember("Arguments", shortcut, arguments);
shortcutType.InvokeSetMember("Hotkey", shortcut, hotkey);
shortcutType.InvokeSetMember("WindowStyle", shortcut, shortcutWindowStyle);
shortcutType.InvokeSetMember("Description", shortcut, description);
shortcutType.InvokeSetMember("IconLocation", shortcut, iconLocation);
shortcutType.InvokeMethod("Save", shortcut);
}
return Path.Combine(Environment.CurrentDirectory, linkFileName);
}
private static object InvokeSetMember(
this Type type,
string methodName,
object targetInstance,
params object[] arguments) {
return type.InvokeMember(
methodName,
BindingFlags.Public | BindingFlags.Instance | BindingFlags.SetProperty,
null,
targetInstance,
arguments);
}
private static object InvokeMethod(
this Type type,
string methodName,
object targetInstance,
params object[] arguments) {
return type.InvokeMember(
methodName,
BindingFlags.Public | BindingFlags.Instance | BindingFlags.InvokeMethod,
null,
targetInstance,
arguments);
}
public static void CreateDesktopShortcut(string programPath) {
const string shortcutName = "ILAC TARIF.lnk";
if (File.Exists($@"{Constants.DesktopLocation}\{shortcutName}")) {
File.Delete($@"{Constants.DesktopLocation}\{shortcutName}");
}
CreateShortcut(shortcutName, programPath, "", "-m");
File.Move(shortcutName, $@"{Constants.DesktopLocation}\{shortcutName}");
}
#region Constants
/// <summary>
/// Default shortcut extension
/// </summary>
private const string DefaultShortcutExtension = ".lnk";
private const string WscriptShellName = "WScript.Shell";
#endregion
}