public static void PinUnpinTaskBar(string filePath, bool pin) { if (!string.IsNullOrEmpty(filePath) && File.Exists(filePath)) { string directory = Path.GetDirectoryName(filePath); string filename = Path.GetFileName(filePath); Shell shell = new ShellClass(); Folder folder = shell.NameSpace(directory); FolderItem folderItem = folder.ParseName(filename); FolderItemVerbs verbs = folderItem.Verbs(); for (int i = 0; i < verbs.Count; i++) { FolderItemVerb verb = verbs.Item(i); string verbName = verb.Name.Replace(@"&", ""); if ((pin && verbName.Equals("pin to taskbar", StringComparison.InvariantCultureIgnoreCase)) || (!pin && verbName.Equals("unpin from taskbar", StringComparison.InvariantCultureIgnoreCase))) { verb.DoIt(); return; } } } }
private static void PinUnpinStartMenu(string filePath, bool pin) { if (!File.Exists(filePath)) { throw new FileNotFoundException(filePath); } // create the shell application object Shell shellApplication = new Shell(); string path = Path.GetDirectoryName(filePath); string fileName = Path.GetFileName(filePath); Folder directory = shellApplication.NameSpace(path); FolderItem link = directory.ParseName(fileName); FolderItemVerbs verbs = link.Verbs(); for (int i = 0; i < verbs.Count; i++) { FolderItemVerb verb = verbs.Item(i); string verbName = verb.Name.Replace(@"&", string.Empty).ToLower(); if ((pin && verbName.Equals("pin to start menu")) || (!pin && verbName.Equals("unpin from start menu"))) { verb.DoIt(); } } shellApplication = null; }
//static void Main(string[] args) //{ // string importFiles = "C:\\Users\\Varsha.Ravindra\\Documents\\Registry"; // Dictionary<object, object> importJson = new Dictionary<object, object>(); // importJson = JsonConvert.DeserializeObject<Dictionary<object, object>>(File.ReadAllText(Path.Combine(importFiles, "TaskbarPinned.json"), Encoding.UTF8)); // Console.WriteLine(); // foreach (KeyValuePair<object, object> val in importJson) // { // string targetPath = val.Value.ToString(); // if (string.IsNullOrEmpty(targetPath)) // { // continue; // } // else if (targetPath.Contains("C:\\Users\\")) // { // string userFolder = Directory.GetParent(Directory.GetParent(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)).ToString()).ToString(); // List<string> splitTarget = new List<string>(); // splitTarget = targetPath.Split('\\').ToList(); // splitTarget.RemoveRange(0, 3); // string restPath = string.Join("\\", splitTarget); // targetPath = Path.Combine(userFolder, restPath); // } // if (System.IO.File.Exists(targetPath)) // { // Console.WriteLine("EXISTS : {0}", targetPath); // PinUnpinTaskbar(targetPath, true); // Console.WriteLine(); // } // } // Console.ReadKey(); //} static void Main(string[] args) { string filePath = @"C:\Program Files\Notepad++\notepad++.exe"; if (!File.Exists(filePath)) { throw new FileNotFoundException(filePath); } // create the shell application object Type t = Type.GetTypeFromProgID("Shell.Application"); dynamic shellApplication = Activator.CreateInstance(t); string path = Path.GetDirectoryName(filePath); string fileName = Path.GetFileName(filePath); Folder directory = shellApplication.NameSpace(path); FolderItem link = directory.ParseName(fileName); FolderItemVerbs verbs = link.Verbs(); for (int i = 0; i < verbs.Count; i++) { FolderItemVerb verb = verbs.Item(i); string verbName = verb.Name; Console.WriteLine(verbName); if (verbName.Equals("pin to taskbar")) { verb.DoIt(); } } shellApplication = null; Console.ReadKey(); }
protected override void OnClick(EventArgs e) { base.OnClick(e); // try { _verb.DoIt(); } catch {} }
private static bool PinUnpinTaskBar(string filePath, bool pin) { bool success = false; if (!File.Exists(filePath)) { throw new FileNotFoundException(filePath); } // create the shell application object Shell shellApplication = new Shell(); string path = Path.GetDirectoryName(filePath); string fileName = Path.GetFileName(filePath); Folder directory = shellApplication.NameSpace(path); FolderItem link = directory.ParseName(fileName); FolderItemVerbs verbs = link.Verbs(); for (int i = 0; i < verbs.Count; i++) { FolderItemVerb verb = verbs.Item(i); string verbName = verb.Name.Replace(@"&", string.Empty).ToLower(); System.Diagnostics.Trace.WriteLine(string.Format("Verb:{0}", verbName)); if ((pin && verbName.Equals("pin to taskbar")) || (!pin && verbName.Equals("unpin from taskbar"))) { verb.DoIt(); success = true; } } shellApplication = null; return(success); }
static void Main(string[] args) { //string l_strFilePath = @"C:\Program Files\Notepad++\notepad++.exe"; //if (!File.Exists(l_strFilePath)) throw new FileNotFoundException(l_strFilePath); //int MAX_PATH = 255; //var actionIndex = true ? 5386 : 5387; //StringBuilder szPinToStartLocalized = new StringBuilder(MAX_PATH); //IntPtr hShell32 = LoadLibrary("Shell32.dll"); //LoadString(hShell32, (uint)actionIndex, szPinToStartLocalized, MAX_PATH); //string localizedVerb = szPinToStartLocalized.ToString(); //string path = Path.GetDirectoryName(l_strFilePath); //string fileName = Path.GetFileName(l_strFilePath); //dynamic shellApplication = Activator.CreateInstance(Type.GetTypeFromProgID("Shell.Application")); //dynamic directory = shellApplication.NameSpace(path); //dynamic link = directory.ParseName(fileName); //dynamic verbs = link.Verbs(); //for (int i = 0; i < verbs.Count(); i++) //{ // dynamic verb = verbs.Item(i); // if (verb.Name.Equals(localizedVerb)) // { // verb.DoIt(); // return; // } //} RegistryKey TaskbarPinned = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\Windows.taskbarpin"); string explorerCommandHandler = TaskbarPinned.GetValue("ExplorerCommandHandler", 0).ToString(); Console.WriteLine(explorerCommandHandler); string filePath = @"C:\Program Files\Notepad++\notepad++.exe"; if (!File.Exists(filePath)) { throw new FileNotFoundException(filePath); } string path = Path.GetDirectoryName(filePath); string fileName = Path.GetFileName(filePath); RegistryKey createKey = Registry.CurrentUser.OpenSubKey(@"Software\Classes\*\shell", true); createKey.CreateSubKey("{:}", true); RegistryKey openNewKey = Registry.CurrentUser.OpenSubKey(@"Software\Classes\*\shell\{:}\", true); openNewKey.SetValue("ExplorerCommandHandler", explorerCommandHandler, RegistryValueKind.String); Type t = Type.GetTypeFromProgID("Shell.Application"); dynamic shellApplication = Activator.CreateInstance(t); Folder directory = shellApplication.NameSpace(path); FolderItem link = directory.ParseName(fileName); FolderItemVerbs verbs = link.Verbs(); for (int i = 0; i < verbs.Count; i++) { FolderItemVerb verb = verbs.Item(i); string verbName = verb.Name.Replace("&", ""); Console.WriteLine(verbName); //verbName.Equals("pin to taskbar") || verbName.Equals("taskbarpin") || if (verbName.Equals("{:}")) { Console.WriteLine("DO IT"); verb.DoIt(); } } createKey.DeleteSubKey("{:}"); Console.ReadKey(); }