예제 #1
0
        private void AppMenuItem_Click(object sender, EventArgs e)
        {
            var selectedItem = appsBox.SelectedItem.ToString();

            if (string.IsNullOrEmpty(selectedItem))
            {
                return;
            }
            var appData = CacheData.FindAppData(selectedItem);

            if (appData == default)
            {
                return;
            }
            var owner = sender as ToolStripMenuItem;

            switch (owner?.Name)
            {
            case nameof(appMenuItem1):
                appData.StartApplication(true);
                break;

            case nameof(appMenuItem2):
                appData.StartApplication(true, true);
                break;

            case nameof(appMenuItem3):
                appData.OpenLocation();
                break;

            case nameof(appMenuItem4):
                var linkPath = PathEx.Combine(Environment.SpecialFolder.Desktop, selectedItem);
                var created  = FileEx.CreateShellLink(appData.FilePath, linkPath);
                MessageBoxEx.CenterMousePointer = !ClientRectangle.Contains(PointToClient(MousePosition));
                MessageBoxEx.Show(this, Language.GetText(created ? nameof(en_US.appMenuItem4Msg0) : nameof(en_US.appMenuItem4Msg1)), Resources.GlobalTitle, MessageBoxButtons.OK, created ? MessageBoxIcon.Asterisk : MessageBoxIcon.Warning);
                break;

            case nameof(appMenuItem7):
                if (MessageBoxEx.Show(this, string.Format(CultureInfo.InvariantCulture, Language.GetText(nameof(en_US.appMenuItem7Msg)), selectedItem), Resources.GlobalTitle, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    MessageBoxEx.CenterMousePointer = !ClientRectangle.Contains(PointToClient(MousePosition));
                    if (appData.RemoveApplication(this))
                    {
                        appsBox.Items.RemoveAt(appsBox.SelectedIndex);
                        if (appsBox.SelectedIndex < 0)
                        {
                            appsBox.SelectedIndex = 0;
                        }
                    }
                }
                else
                {
                    MessageBoxEx.CenterMousePointer = !ClientRectangle.Contains(PointToClient(MousePosition));
                    MessageBoxEx.Show(this, Language.GetText(nameof(en_US.OperationCanceledMsg)), Resources.GlobalTitle, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                }
                break;
            }
        }
예제 #2
0
        internal static void UpdateStartMenuShortcuts(IEnumerable <string> longAppNames)
        {
            var appNames = longAppNames?.ToArray();

            if (appNames?.Any() != true)
            {
                return;
            }
            try
            {
                var startMenuDir = PathEx.Combine(Environment.SpecialFolder.StartMenu, "Programs");
#if x86
                var shortcutPath = Path.Combine(startMenuDir, "Apps Launcher.lnk");
#else
                var shortcutPath = Path.Combine(startMenuDir, "Apps Launcher (64-bit).lnk");
#endif
                if (Directory.Exists(startMenuDir))
                {
                    var shortcuts = Directory.GetFiles(startMenuDir, "Apps Launcher*.lnk", SearchOption.TopDirectoryOnly);
                    if (shortcuts.Length > 0)
                    {
                        foreach (var shortcut in shortcuts)
                        {
                            File.Delete(shortcut);
                        }
                    }
                }
                if (!Directory.Exists(startMenuDir))
                {
                    Directory.CreateDirectory(startMenuDir);
                }
                FileEx.CreateShellLink(EnvironmentEx.GetVariablePathFull(PathEx.LocalPath, false), shortcutPath);
                startMenuDir = Path.Combine(startMenuDir, "Portable Apps");
                if (Directory.Exists(startMenuDir))
                {
                    var shortcuts = Directory.GetFiles(startMenuDir, "*.lnk", SearchOption.TopDirectoryOnly);
                    if (shortcuts.Length > 0)
                    {
                        foreach (var shortcut in shortcuts)
                        {
                            File.Delete(shortcut);
                        }
                    }
                }
                if (!Directory.Exists(startMenuDir))
                {
                    Directory.CreateDirectory(startMenuDir);
                }
                Parallel.ForEach(appNames, x => FileEx.CreateShellLink(EnvironmentEx.GetVariablePathFull(CacheData.FindAppData(x)?.FilePath, false, false), Path.Combine(startMenuDir, x)));
            }
            catch (Exception ex)
            {
                Log.Write(ex);
            }
        }
예제 #3
0
        internal static void Enable(bool enabled, bool quiet = false)
        {
            if (!Elevation.IsAdministrator)
            {
                using (var process = ProcessEx.Start(PathEx.LocalPath, $"{ActionGuid.SystemIntegration} {enabled}", true, false))
                    if (process?.HasExited == false)
                    {
                        process.WaitForExit();
                    }
                return;
            }
            var varKey = "SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment";
            var varDir = Reg.ReadString(Registry.LocalMachine, varKey, Settings.EnvironmentVariable);
            var curDir = PathEx.LocalDir;

            if (!enabled || !varDir.EqualsEx(curDir))
            {
                var curPath    = EnvironmentEx.GetVariablePathFull(PathEx.LocalPath, false);
                var sendToPath = PathEx.Combine(Environment.SpecialFolder.SendTo, "Apps Launcher.lnk");
                if (enabled)
                {
                    Reg.Write(Registry.LocalMachine, varKey, Settings.EnvironmentVariable, curDir);
                    FileEx.CreateShellLink(curPath, sendToPath);
                }
                else
                {
                    Reg.RemoveEntry(Registry.LocalMachine, varKey, Settings.EnvironmentVariable);
                    PathEx.ForceDelete(sendToPath);
                }
                if (WinApi.NativeHelper.SendNotifyMessage((IntPtr)0xffff, (uint)WinApi.WindowMenuFlags.WmSettingChange, (UIntPtr)0, "Environment"))
                {
                    foreach (var baseKey in new[]
                    {
                        "*",
                        "Folder"
                    })
                    {
                        varKey = Path.Combine(baseKey, "shell\\portableapps");
                        if (enabled)
                        {
                            if (string.IsNullOrWhiteSpace(Reg.ReadString(Registry.ClassesRoot, varKey, null)))
                            {
                                Reg.Write(Registry.ClassesRoot, varKey, null, Language.GetText(nameof(en_US.shellText)));
                            }
                            Reg.Write(Registry.ClassesRoot, varKey, "Icon", $"\"{PathEx.LocalPath}\"");
                        }
                        else
                        {
                            Reg.RemoveSubKey(Registry.ClassesRoot, varKey);
                        }
                        varKey = Path.Combine(varKey, "command");
                        if (enabled)
                        {
                            Reg.Write(Registry.ClassesRoot, varKey, null, $"\"{PathEx.LocalPath}\" \"%1\"");
                        }
                        else
                        {
                            Reg.RemoveSubKey(Registry.ClassesRoot, varKey);
                        }
                    }
                    if (enabled)
                    {
                        if (TaskBar.Pin(PathEx.LocalPath))
                        {
                            var pinnedDir = PathEx.Combine(Environment.SpecialFolder.ApplicationData, "Microsoft", "Internet Explorer", "Quick Launch", "User Pinned", "TaskBar");
                            foreach (var file in Directory.GetFiles(pinnedDir, "*.lnk", SearchOption.TopDirectoryOnly))
                            {
                                if (!string.Equals(FileEx.GetShellLinkTarget(file), PathEx.LocalPath, StringComparison.CurrentCultureIgnoreCase))
                                {
                                    continue;
                                }
                                ProcessEx.SendHelper.Delete(file);
                                Environment.SetEnvironmentVariable(Settings.EnvironmentVariable, curDir, EnvironmentVariableTarget.Process);
                                FileEx.CreateShellLink(curPath, file);
                                break;
                            }
                        }
                    }
                    else
                    {
                        TaskBar.Unpin(PathEx.LocalPath);
                    }

                    if (enabled)
                    {
                        using (var process = ProcessEx.Start(PathEx.LocalPath, ActionGuid.FileTypeAssociationAll, true, false))
                            if (process?.HasExited == false)
                            {
                                process.WaitForExit();
                            }
                    }

                    if (!quiet)
                    {
                        MessageBox.Show(Language.GetText(nameof(en_US.OperationCompletedMsg)), string.Empty, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    return;
                }
            }
            if (!quiet)
            {
                MessageBox.Show(Language.GetText(nameof(en_US.OperationCanceledMsg)), string.Empty, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        internal static void Enable(bool enabled, bool quiet = false)
        {
            if (!Elevation.IsAdministrator)
            {
                using (var process = ProcessEx.Start(PathEx.LocalPath, $"{ActionGuid.SystemIntegration} {enabled} {quiet}", true, false))
                    if (process?.HasExited == false)
                    {
                        process.WaitForExit();
                    }
                return;
            }
            var varKey = "SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment";
            var varDir = Reg.ReadString(Registry.LocalMachine, varKey, Settings.EnvironmentVariable);
            var curDir = PathEx.LocalDir;

            if (!enabled || !varDir.EqualsEx(curDir))
            {
                var curPath    = EnvironmentEx.GetVariableWithPath(PathEx.LocalPath, false);
                var sendToPath = PathEx.Combine(Environment.SpecialFolder.SendTo, "Apps Launcher.lnk");
                if (enabled)
                {
                    Reg.Write(Registry.LocalMachine, varKey, Settings.EnvironmentVariable, curDir);
                    FileEx.CreateShellLink(curPath, sendToPath);
                }
                else
                {
                    Reg.RemoveEntry(Registry.LocalMachine, varKey, Settings.EnvironmentVariable);
                    PathEx.ForceDelete(sendToPath);
                }
                if (WinApi.NativeHelper.SendNotifyMessage((IntPtr)0xffff, (uint)WinApi.WindowMenuFlags.WmSettingChange, (UIntPtr)0, "Environment"))
                {
                    foreach (var baseKey in new[]
                    {
                        "*",
                        "Folder"
                    })
                    {
                        varKey = Path.Combine(baseKey, "shell\\portableapps");
                        if (enabled)
                        {
                            if (string.IsNullOrWhiteSpace(Reg.ReadString(Registry.ClassesRoot, varKey, null)))
                            {
                                Reg.Write(Registry.ClassesRoot, varKey, null, Language.GetText(nameof(en_US.shellText)));
                            }
                            Reg.Write(Registry.ClassesRoot, varKey, "Icon", $"\"{PathEx.LocalPath}\"");
                        }
                        else
                        {
                            Reg.RemoveSubKey(Registry.ClassesRoot, varKey);
                        }
                        varKey = Path.Combine(varKey, "command");
                        if (enabled)
                        {
                            Reg.Write(Registry.ClassesRoot, varKey, null, $"\"{PathEx.LocalPath}\" \"%1\"");
                        }
                        else
                        {
                            Reg.RemoveSubKey(Registry.ClassesRoot, varKey);
                        }
                    }
                    var desktopPath = PathEx.Combine(Environment.SpecialFolder.Desktop, "Apps Launcher.lnk");
                    if (enabled)
                    {
                        FileEx.CreateShellLink(curPath, desktopPath);
                    }
                    else
                    {
                        PathEx.ForceDelete(sendToPath);
                    }

                    if (enabled)
                    {
                        using (var process = ProcessEx.Start(PathEx.LocalPath, ActionGuid.FileTypeAssociationAll, true, false))
                            if (process?.HasExited == false)
                            {
                                process.WaitForExit();
                            }
                    }

                    if (!quiet)
                    {
                        MessageBox.Show(Language.GetText(nameof(en_US.OperationCompletedMsg)), string.Empty, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    return;
                }
            }
            if (!quiet)
            {
                MessageBox.Show(Language.GetText(nameof(en_US.OperationCanceledMsg)), string.Empty, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
예제 #5
0
        private void AppMenuItem_Click(object sender, EventArgs e)
        {
            if (appsListView.SelectedItems.Count == 0)
            {
                return;
            }
            var selectedItem = appsListView.SelectedItems.Cast <ListViewItem>().FirstOrDefault();

            if (selectedItem == default)
            {
                return;
            }
            var appData = CacheData.FindAppData(selectedItem.Text);

            if (appData == default)
            {
                return;
            }
            var owner = sender as ToolStripMenuItem;

            switch (owner?.Name)
            {
            case nameof(appMenuItem1):
            case nameof(appMenuItem2):
            case nameof(appMenuItem3):
                if (Opacity > 0)
                {
                    Opacity = 0;
                }
                switch (owner.Name)
                {
                case nameof(appMenuItem1):
                case nameof(appMenuItem2):
                    PreventClosure = true;
                    appData.StartApplication(true, owner.Name.EqualsEx(nameof(appMenuItem2)));
                    break;

                case nameof(appMenuItem3):
                    PreventClosure = true;
                    appData.OpenLocation(true);
                    break;
                }
                break;

            case nameof(appMenuItem4):
            {
                var linkPath = PathEx.Combine(Environment.SpecialFolder.Desktop, selectedItem.Text);
                var created  = FileEx.CreateShellLink(appData.FilePath, linkPath);
                MessageBoxEx.CenterMousePointer = !ClientRectangle.Contains(PointToClient(MousePosition));
                MessageBoxEx.Show(this, Language.GetText(created ? nameof(en_US.appMenuItem4Msg0) : nameof(en_US.appMenuItem4Msg1)), Resources.GlobalTitle, MessageBoxButtons.OK, created ? MessageBoxIcon.Asterisk : MessageBoxIcon.Warning);
                break;
            }

            case nameof(appMenuItem6):
                if (appsListView.SelectedItems.Count > 0)
                {
                    if (!appsListView.LabelEdit)
                    {
                        appsListView.LabelEdit = true;
                    }
                    selectedItem.BeginEdit();
                }
                break;

            case nameof(appMenuItem7):
                MessageBoxEx.CenterMousePointer = !ClientRectangle.Contains(PointToClient(MousePosition));
                if (MessageBoxEx.Show(this, string.Format(CultureInfo.InvariantCulture, Language.GetText(nameof(en_US.appMenuItem7Msg)), selectedItem.Text), Resources.GlobalTitle, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    MessageBoxEx.CenterMousePointer = !ClientRectangle.Contains(PointToClient(MousePosition));
                    PreventClosure = true;
                    if (appData.RemoveApplication(this))
                    {
                        MenuViewFormUpdate(false);
                    }
                    PreventClosure = false;
                }
                else
                {
                    MessageBoxEx.CenterMousePointer = !ClientRectangle.Contains(PointToClient(MousePosition));
                    MessageBoxEx.Show(this, Language.GetText(nameof(en_US.OperationCanceledMsg)), Resources.GlobalTitle, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                }
                break;

            case nameof(appMenuItem8):
                OpenForm(new SettingsForm(appData));
                break;
            }
            if (MessageBoxEx.CenterMousePointer)
            {
                MessageBoxEx.CenterMousePointer = false;
            }
        }
예제 #6
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.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);
                }
            }
        }