【问题标题】:Invoking windows task manager with 'performance' tab selected选择“性能”选项卡调用 Windows 任务管理器
【发布时间】:2025-12-01 04:40:01
【问题描述】:

我目前正在使用 WPF 中的单击事件调用 Windows 任务管理器。该事件只是执行'Process.Start("taskmgr")。

我的问题是,有没有办法选择在进程启动/显示时选择任务管理器中的哪个选项卡?我希望在引发点击事件时自动选择“性能”选项卡。

感谢您的帮助。

【问题讨论】:

    标签: c# wpf process taskmanager


    【解决方案1】:

    为了扩展 Philipp Schmid 的帖子,我制作了一个小演示:

    将其作为控制台应用程序运行。您需要添加对UIAutomationClientUIAutomationTypes 的引用。

    您(或我,如果您愿意)可以做出的一个可能的改进是最初隐藏窗口,仅在选择了正确的选项卡后才显示它。但是,我不确定这是否可行,因为我不确定AutomationElement.FromHandle 是否能够找到隐藏窗口。

    编辑:至少在我的电脑(Windows 7、32 位、.Net framework 4.0)上,以下代码最初会创建一个隐藏的任务管理器,并在选择正确的选项卡后显示它。选择性能选项卡后,我没有明确显示窗口,因此其中一条自动化线可能会产生副作用。

    using System;
    using System.Diagnostics;
    using System.Windows.Automation;
    
    namespace ConsoleApplication2 {
        class Program {
            static void Main(string[] args) {
                // Kill existing instances
                foreach (Process pOld in Process.GetProcessesByName("taskmgr")) {
                    pOld.Kill();
                }
    
                // Create a new instance
                Process p = new Process();
                p.StartInfo.FileName = "taskmgr";
                p.StartInfo.CreateNoWindow = true;
                p.Start();
    
                Console.WriteLine("Waiting for handle...");
    
                while (p.MainWindowHandle == IntPtr.Zero) ;
    
                AutomationElement aeDesktop = AutomationElement.RootElement;
                AutomationElement aeForm = AutomationElement.FromHandle(p.MainWindowHandle);
                Console.WriteLine("Got handle");
    
                // Get the tabs control
                AutomationElement aeTabs = aeForm.FindFirst(TreeScope.Children,
      new PropertyCondition(AutomationElement.ControlTypeProperty,
        ControlType.Tab));
    
                // Get a collection of tab pages
                AutomationElementCollection aeTabItems = aeTabs.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ControlTypeProperty,
        ControlType.TabItem));
    
                // Set focus to the performance tab
                AutomationElement aePerformanceTab = aeTabItems[3];
                aePerformanceTab.SetFocus();
            }
        }
    }
    

    为什么要销毁以前的任务管理器实例?当实例已经打开时,辅助实例将打开但立即关闭。我的代码没有对此进行检查,因此找到窗口句柄的代码将冻结。

    【讨论】:

    • 感谢您的快速解决方案。知道这可以“从技术上”完成,我将更多地研究自动化,看看我能做什么。
    • 很高兴我能帮上忙。上面的代码应该适合生产使用,只要添加一些错误检查即可。在您提出问题之前,我实际上对这些托管自动化设备一无所知。所以,谢谢你不小心把我介绍给他们:)
    • 另外,如果您使用我的代码,请务必将 while 循环替换为...更好的东西。这有可能永远循环
    【解决方案2】:

    虽然 taskmgr.exe 没有任何命令行参数来指定所选选项卡,但您可以使用 Windows UI Automation 来“导航”到性能选项卡。

    【讨论】:

      【解决方案3】:

      很遗憾,taskmgr.exe 不支持任何命令行参数。

      运行时,它将始终激活上次关闭时处于活动状态的选项卡。

      【讨论】:

      • +1 - 一个丑陋的技巧是通过HKCU/Software/Microsoft/Windows NT/CurrentVersion/TaskManager中的注册表更改最后一个活动选项卡
      • taskmgr 支持 CLI 参数。
      【解决方案4】:

      从 Windows 10 build 18305 开始,您现在可以设置首选选项卡以默认打开任务管理器。

      更新:

      • 点击开始菜单并在搜索框中输入“Windows Update”
      • 选择“Windows 更新设置”
      • 在左侧面板中单击“预览构建”
      • 现在点击“检查”。
      • 下载新版本。

      更新后,更改Win注册表项中StartUpTab的dword值: HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\TaskManager

      0 – Processes tab
      1 – Performance tab
      2 – App history tab
      3 – Startup tab
      4 – Users tab
      5 – Details tab
      6 – Services tab
      

      Win CMD:
      reg add HKCU\Software\Microsoft\Windows\CurrentVersion\TaskManager /v "startup" /t REG_DWORD /d "1"

      此(实验性)功能仅适用于部分 Windows 预览体验成员。

      Win 10 的旧版本不支持除“启动”之外的其他选项卡:
      taskmgr /4 /startup

      重置:
      reg delete HKCU\Software\Microsoft\Windows\CurrentVersion\TaskManager /v "Preferences" /f

      确认修改密钥:
      REG ADD "HKCU\Software\Microsoft\Windows\CurrentVersion\Applets\Regedit" /v "LastKey" /d "HKCU\Software\Microsoft\Windows\CurrentVersion\TaskManager" /f & regedit

      在 Win 10 CMD 中测试

      【讨论】: