示例#1
0
        private async Task <List <TreeNode> > AddProcesses(int[] processIDs, bool processChildren, ImageList il)
        {
            List <Tuple <Process, Icon> > processes = null;
            await Task.Run(() => processes = FindProcesses(processIDs));

            var results = new List <TreeNode>();

            if (processes == null)
            {
                return(results);
            }

            foreach (var process in processes)
            {
                var mainPrName = string.IsNullOrEmpty(process.Item1.MainWindowTitle) ? process.Item1.ProcessName : process.Item1.MainWindowTitle;
                var node       = new TreeNode(mainPrName)
                {
                    SelectedImageKey = mainPrName,
                    ImageKey         = mainPrName,
                    Tag = process.Item1
                };
                results.Add(node);

                if (process.Item2 != null)
                {
                    il.Images.Add(mainPrName, process.Item2);
                }

                if (processChildren)
                {
                    try
                    {
                        var children = ProcessTools.GetChildProcesses(process.Item1.Id);

                        var childProcesses = await AddProcesses(children.ToArray(), processChildren, il);

                        node.Nodes.AddRange(childProcesses.ToArray());
                    }
                    catch (Exception ex)
                    {
                        // Ignore, probably the process exited by now. The child nodes are not important
                        Console.WriteLine(ex);
                    }
                }
            }
            return(results);
        }
        /// <summary>
        ///     Automate uninstallation of an NSIS uninstaller.
        /// </summary>
        /// <param name="uninstallerCommand">Command line used to launch the NSIS uninstaller. (Usually path to uninstall.exe.)</param>
        /// <param name="statusCallback">Information about the process is relayed here</param>
        public static void UninstallNsisQuietly(string uninstallerCommand, Action <string> statusCallback)
        {
            Process     pr  = null;
            Application app = null;

            try
            {
                pr = Process.Start(uninstallerCommand);
                if (pr == null)
                {
                    throw new IOException(Localization.Message_Automation_ProcessFailedToStart);
                }

                // NSIS uninstallers are first extracted by the executable to a temporary directory, and then ran from there.
                // Wait for the extracting exe to close and grab the child process that it started.
                statusCallback(Localization.Message_Automation_WaitingForNsisExtraction);
                pr.WaitForExit();

                // Attempt to get the extracted exe by looking up child processes, might not work in some cases
                var prs = ProcessTools.GetChildProcesses(pr.Id).FirstOrDefault();

                if (prs != 0)
                {
                    app = Application.Attach(prs);
                }
                else
                {
                    // Get all processes with name in format [A-Z]u_ (standard NSIS naming scheme, e.g. "Au_.exe")
                    // and select the last one to launch. (Most likely to be ours)
                    var uninstallProcess = Process.GetProcesses()
                                           .Where(x => x.ProcessName.Length == 3 && x.ProcessName.EndsWith("u_", StringComparison.Ordinal))
                                           .OrderByDescending(x => x.StartTime).First();
                    app = Application.Attach(uninstallProcess);
                }
            }
            catch (Exception e)
            {
                throw new AutomatedUninstallException(Localization.Message_Automation_Failed, e,
                                                      uninstallerCommand, app?.Process ?? pr);
            }

            if (app != null)
            {
                AutomatizeApplication(app, statusCallback);
            }
        }
        /// <summary>
        ///     Automate uninstallation of an NSIS uninstaller.
        /// </summary>
        /// <param name="uninstallerCommand">Command line used to launch the NSIS uninstaller. (Usually path to uninstall.exe.)</param>
        public static void UninstallNsisQuietly(string uninstallerCommand)
        {
            Process     pr  = null;
            Application app = null;

            try
            {
                pr = Process.Start(uninstallerCommand);
                if (pr == null)
                {
                    throw new IOException("Process failed to start");
                }

                // NSIS uninstallers are first extracted by the executable to a temporary directory, and then ran from there.
                // Wait for the extracting exe to close and grab the child process that it started.
                pr.WaitForExit();

                // Attempt to get the extracted exe by looking up child processes, might not work in some cases
                var prs = ProcessTools.GetChildProcesses(pr.Id).FirstOrDefault();

                if (prs != 0)
                {
                    app = Application.Attach(prs);
                }
                else
                {
                    // Get all processes with name in format [A-Z]u_ (standard NSIS naming scheme, e.g. "Au_.exe")
                    // and select the last one to launch. (Most likely to be ours)
                    var uninstallProcess = Process.GetProcesses()
                                           .Where(x => x.ProcessName.Length == 3 && x.ProcessName.EndsWith("u_"))
                                           .OrderByDescending(x => x.StartTime).First();
                    app = Application.Attach(uninstallProcess);
                }

                WaitForApplication(app);

                // Use UI item counts to identify TODO Check using something better
                var seenWindows = new List <int>();

                while (!app.HasExited)
                {
                    // NSIS uninstallers always have only one window open (by default)
                    var windows = app.GetWindows();
                    var target  = windows.First();

                    WaitForWindow(target);

                    // BUG target.IsClosed changes to true if window gets minimized?
                    while (!target.IsClosed)
                    {
                        TryClickNextNsisButton(target);
                        WaitForWindow(target);

                        ProcessNsisPopups(app, target, seenWindows);
                    }

                    WaitForApplication(app);
                }
            }
            catch (Exception e)
            {
                throw new AutomatedUninstallException("Automatic uninstallation failed", e,
                                                      uninstallerCommand, app?.Process ?? pr);
            }
        }
        private void SetNodes(int[] processIDs, bool processChildren)
        {
            var results = new List <TreeNode>();
            var il      = new ImageList();

            il.Images.Add(DefaultImageKey, SystemIcons.Application);
            treeView1.ImageKey = DefaultImageKey;

            foreach (var id in processIDs)
            {
                try
                {
                    var p = Process.GetProcessById(id);
                    if (p.HasExited)
                    {
                        continue;
                    }

                    var mainPrName = string.IsNullOrEmpty(p.MainWindowTitle) ? p.ProcessName : p.MainWindowTitle;
                    var node       = new TreeNode(mainPrName)
                    {
                        SelectedImageKey = mainPrName,
                        ImageKey         = mainPrName,
                        Tag = p
                    };
                    results.Add(node);

                    try
                    {
                        var ico = DrawingTools.ExtractAssociatedIcon(p.MainModule.FileName);
                        if (ico != null)
                        {
                            il.Images.Add(mainPrName, ico);
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex);
                    }

                    if (!processChildren)
                    {
                        continue;
                    }
                    try
                    {
                        var children = ProcessTools.GetChildProcesses(id);
                        node.Nodes.AddRange(children.Select(x =>
                        {
                            var pr   = Process.GetProcessById(x);
                            var name = string.IsNullOrEmpty(pr.MainWindowTitle)
                                ? pr.ProcessName
                                : pr.MainWindowTitle;
                            return(new TreeNode(name)
                            {
                                SelectedImageKey = mainPrName,
                                ImageKey = mainPrName,
                                Tag = pr
                            });
                        }).ToArray());
                    }
                    catch (Exception ex)
                    {
                        // Ignore, probably the process exited by now. The child nodes are not important
                        Console.WriteLine(ex);
                    }
                }
                catch (Exception ex)
                {
                    // Probably the main process exited, remove it from the task
                    Console.WriteLine(ex);
                }
            }

            treeView1.Nodes.Clear();
            var prev = treeView1.ImageList;

            treeView1.ImageList = il;
            prev?.Dispose();

            treeView1.Nodes.AddRange(results.ToArray());
        }