Last active
August 29, 2015 13:57
-
-
Save fearthecowboy/9690144 to your computer and use it in GitHub Desktop.
Pin or unpin items from the taskbar programatically
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Copyright (c) Microsoft Corporation. All rights reserved. | |
// Licensed under the Apache License, Version 2.0 (the "License"); | |
// you may not use this file except in compliance with the License. | |
// You may obtain a copy of the License at | |
// http://www.apache.org/licenses/LICENSE-2.0 | |
// | |
// Unless required by applicable law or agreed to in writing, software | |
// distributed under the License is distributed on an "AS IS" BASIS, | |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
// See the License for the specific language governing permissions and | |
// limitations under the License. | |
// | |
namespace TaskbarHacks { | |
using System; | |
using System.Collections; | |
using System.IO; | |
using System.Linq; | |
using System.Runtime.InteropServices; | |
using System.Text; | |
public class Taskbar { | |
private static readonly string _pin; | |
private static readonly string _unpin; | |
static Taskbar() { | |
var buffer = new StringBuilder(0x100); | |
IntPtr instance = LoadLibrary("shell32.dll"); | |
LoadString(instance, 0x150a, buffer, buffer.Capacity); | |
_pin = buffer.ToString(); | |
LoadString(instance, 0x150b, buffer, buffer.Capacity); | |
_unpin = buffer.ToString(); | |
FreeLibrary(instance); | |
} | |
[DllImport("kernel32")] | |
private static extern IntPtr LoadLibrary(string fileName); | |
[DllImport("user32")] | |
private static extern int LoadString(IntPtr instance, uint stringId, StringBuilder buffer, int bufferSize); | |
[DllImport("kernel32")] | |
private static extern bool FreeLibrary(IntPtr Instance); | |
internal static void Pin(string lnkPath) { | |
DoVerbOnPath(lnkPath, _pin); | |
} | |
internal static void Unpin(string lnkPath) { | |
DoVerbOnPath(lnkPath, _unpin); | |
} | |
private static void DoVerbOnPath(string lnkPath, string vName) { | |
if (string.IsNullOrEmpty(lnkPath)) { | |
throw new ArgumentNullException("lnkPath"); | |
} | |
if (string.IsNullOrEmpty(vName)) { | |
throw new ArgumentNullException("vName"); | |
} | |
if (!File.Exists(lnkPath)) { | |
throw new FileNotFoundException("Target Path Not Found", lnkPath); | |
} | |
dynamic shell = Activator.CreateInstance(Type.GetTypeFromProgID("Shell.Application")); | |
var folder = Path.GetDirectoryName(lnkPath); | |
var name = Path.GetFileName(lnkPath); | |
var directory = shell.NameSpace(folder); | |
var link = directory.ParseName(name); | |
var v = (from dynamic verb in ((IEnumerable) (link.Verbs)) | |
where ((string) verb.Name).Equals(vName, StringComparison.OrdinalIgnoreCase) | |
select verb).FirstOrDefault(); | |
if (v != null) { | |
v.DoIt(); | |
} | |
Marshal.ReleaseComObject(shell); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment