Ejemplo n.º 1
0
 private void Button_Click(object sender, EventArgs e)
 {
     if (!(ParentForm is IconBrowserDialog dialog))
     {
         return;
     }
     if (int.TryParse(_button.Text, out var index))
     {
         var path = EnvironmentEx.GetVariablePathFull(_file, false, false);
         dialog.IconPath = path;
         if (path.Any(char.IsSeparator))
         {
             path = $"\"{path}\"";
         }
         dialog.IconId = index;
         dialog.Text   = $@"{path},{index}";
     }
     dialog.Close();
 }
Ejemplo n.º 2
0
        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.ToString();

                /*
                 * 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.GetVariablePathFull(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);
                }
            }
        }