Repository navigation
Expand file tree
/
Copy pathTaskbarHelper.cs
More file actions
70 lines (57 loc) · 2.36 KB
/
Copy pathTaskbarHelper.cs
File metadata and controls
70 lines (57 loc) · 2.36 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
using System;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
namespace Squirrel
{
public static class TaskbarHelper
{
public static bool IsPinnedToTaskbar(string executablePath)
{
var taskbarPath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
"Microsoft\\Internet Explorer\\Quick Launch\\User Pinned\\TaskBar");
return Directory
.GetFiles(taskbarPath, "*.lnk")
.Select(pinnedShortcut => new ShellLink(pinnedShortcut))
.Any(shortcut => String.Equals(shortcut.Target, executablePath, StringComparison.OrdinalIgnoreCase));
}
public static void PinToTaskbar(string executablePath)
{
pinUnpin(executablePath, "pin to taskbar");
if (!IsPinnedToTaskbar(executablePath)) {
throw new Exception("Pinning executable to taskbar failed.");
}
}
public static void UnpinFromTaskbar(string executablePath)
{
pinUnpin(executablePath, "unpin from taskbar");
if (IsPinnedToTaskbar(executablePath)) {
throw new Exception("Executable is still pinned to taskbar.");
}
}
static void pinUnpin(string executablePath, string verbToExecute)
{
if (!File.Exists(executablePath)) {
throw new FileNotFoundException(executablePath);
}
dynamic shellApplication = Activator.CreateInstance(Type.GetTypeFromProgID("Shell.Application"));
try {
var path = Path.GetDirectoryName(executablePath);
var fileName = Path.GetFileName(executablePath);
dynamic directory = shellApplication.NameSpace(path);
dynamic link = directory.ParseName(fileName);
dynamic verbs = link.Verbs();
for (var i = 0; i < verbs.Count(); i++) {
dynamic verb = verbs.Item(i);
string verbName = verb.Name.Replace(@"&", String.Empty).ToLower();
if (verbName.Equals(verbToExecute)) {
verb.DoIt();
}
}
} finally {
Marshal.ReleaseComObject(shellApplication);
}
}
}
}