/// <summary> /// Gets the link path from a pinned item based on its file path. /// </summary> /// <param name="path"> /// The file to get the link. /// </param> public static string GetPinLink(string path) { var file = PathEx.Combine(path); var dir = PathEx.Combine(Environment.SpecialFolder.ApplicationData, "Microsoft\\Internet Explorer\\Quick Launch\\User Pinned\\TaskBar"); foreach (var link in DirectoryEx.EnumerateFiles(dir, "*.lnk")) { var target = ShellLink.GetTarget(link); if (string.IsNullOrEmpty(target)) { continue; } if (target.StartsWith("%", StringComparison.Ordinal)) { target = PathEx.Combine(target); } if (!File.Exists(target)) { continue; } if (target.EqualsEx(file)) { return(link); } } return(null); }
private static bool PinUnpin(string path, bool pin) { if (string.IsNullOrWhiteSpace(path)) { return(false); } var file = PathEx.Combine(path); if (!File.Exists(file)) { return(false); } if (IsPinned(file) == pin) { return(true); } var isPresentWindows = Environment.OSVersion.Version.Major >= 10; var shellKeyPath = default(string); try { if (isPresentWindows) { //ProcessEx.CurrentPrincipal.ChangeName("explorer.exe"); throw new NotSupportedException(); } dynamic shell = Activator.CreateInstance(Type.GetTypeFromProgID("Shell.Application")); var dir = shell.NameSpace(Path.GetDirectoryName(path)); var name = Path.GetFileName(path); var link = dir.ParseName(name); var verbs = link.Verbs(); var sb = new StringBuilder(byte.MaxValue); var lib = WinApi.NativeMethods.LoadLibrary(WinApi.DllNames.Shell32); _ = WinApi.NativeMethods.LoadString(lib, pin ? 0x150au : 0x150bu, sb, 0xff); var verb = sb.ToStringThenClear(); /* * if (!isPresentWindows) * { */ var applied = false; for (var i = 0; i < verbs.Count(); i++) { var e = verbs.Item(i); if ((pin || !e.Name.ContainsEx(verb)) && (!pin || !e.Name.EqualsEx(verb))) { continue; } e.DoIt(); applied = true; break; } if (applied) { goto Done; } //} if (string.IsNullOrWhiteSpace(verb)) { verb = "Toggle Taskbar Pin"; } const string cmdKeyPath = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\CommandStore\\shell\\Windows.taskbarpin"; var cmdHandler = Reg.ReadString(Registry.LocalMachine, cmdKeyPath, "ExplorerCommandHandler"); if (!string.IsNullOrEmpty(cmdHandler)) { shellKeyPath = $"Software\\Classes\\*\\shell\\{verb}"; Reg.Write(Registry.CurrentUser, shellKeyPath, "ExplorerCommandHandler", cmdHandler); } if (Reg.EntryExists(Registry.CurrentUser, shellKeyPath, "ExplorerCommandHandler")) { link.InvokeVerb(verb); } Done: if (!pin) { return(!IsPinned(file)); } var curLink = GetPinLink(path); if (!File.Exists(curLink)) { return(false); } var target = ShellLink.GetTarget(curLink); var envVar = EnvironmentEx.GetVariableWithPath(target, false, false); if (!target.EqualsEx(envVar)) { FileEx.CreateShellLink(file, curLink); } return(true); } catch (Exception ex) when(ex.IsCaught()) { Log.Write(ex); return(false); } finally { /* * if (isPresentWindows) * ProcessEx.CurrentPrincipal.RestoreName(); */ if (!string.IsNullOrEmpty(shellKeyPath)) { Reg.RemoveSubKey(Registry.CurrentUser, shellKeyPath); } } }