You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

开发带筛选功能的类任务管理器:进程信息优化与功能完善求助

Solution for Your Task Manager-like App Optimizations

Hey there! Let’s walk through fixing each of your three requirements to get your app matching Windows Task Manager’s "Details" tab exactly.


1. Add Filtering by Image Path or Description

To implement search functionality that filters processes by image path content (like "Chrome") or description, you’ll want to:

  • Store the full list of processes when your app loads (so you don’t re-fetch processes every time the search term changes)
  • Use LINQ to filter this list based on the search input

Here’s a quick implementation example assuming you’re using a DataGridView to display processes:

// Store all processes once on load
private List<ProcessInfo> _allProcesses;

private void Form_Load(object sender, EventArgs e)
{
    _allProcesses = GetAllProcessesInfo(); // Your existing method to get process data
    dgvProcesses.DataSource = _allProcesses;
}

private void txtSearchBox_TextChanged(object sender, EventArgs e)
{
    string searchTerm = txtSearchBox.Text.Trim().ToLower();
    
    // Filter processes where image path OR description contains the search term
    var filtered = _allProcesses.Where(p => 
        p.ImagePath.ToLower().Contains(searchTerm) || 
        p.Description.ToLower().Contains(searchTerm)
    ).ToList();
    
    dgvProcesses.DataSource = filtered;
}

This keeps the filtering fast and avoids unnecessary system calls to re-fetch processes.


2. Get Process Descriptions Matching Task Manager

Windows Task Manager pulls process descriptions from the file version info of the process’s main executable, not just the process module metadata. To replicate this, use FileVersionInfo to fetch the official FileDescription field:

public static string GetTaskManagerStyleDescription(Process process)
{
    try
    {
        if (!string.IsNullOrEmpty(process.MainModule?.FileName))
        {
            FileVersionInfo fileVersion = FileVersionInfo.GetVersionInfo(process.MainModule.FileName);
            return string.IsNullOrEmpty(fileVersion.FileDescription) 
                ? process.ProcessName 
                : fileVersion.FileDescription;
        }
    }
    catch (UnauthorizedAccessException)
    {
        // Handle cases where you don't have permission to access the process module
        return "Access Denied";
    }
    catch (InvalidOperationException)
    {
        // Handle processes that exited before we could get info
        return "Process Exited";
    }
    
    return string.Empty;
}

This will return the exact same description you see in Task Manager—for example, "Google Chrome" instead of just "chrome".


3. Get Process Usernames Without Extra Methods

While the .NET Process class doesn’t expose a direct UserName property, you can wrap the owner lookup logic into your process data model so you don’t need to call extra methods later. Use System.Management to query the Windows WMI service for the process owner:

First, add a reference to System.Management in your project, then use this helper to fetch the username, and embed it in your process info class:

public class ProcessInfo
{
    public string Name { get; set; }
    public string ImagePath { get; set; }
    public string UserName { get; set; } // No extra methods needed—this is populated on load
    public string Description { get; set; }
}

public List<ProcessInfo> GetAllProcessesInfo()
{
    var processList = new List<ProcessInfo>();
    
    foreach (Process proc in Process.GetProcesses())
    {
        try
        {
            processList.Add(new ProcessInfo
            {
                Name = proc.ProcessName,
                ImagePath = proc.MainModule?.FileName ?? string.Empty,
                Description = GetTaskManagerStyleDescription(proc),
                UserName = GetProcessOwner(proc.Id)
            });
        }
        catch (Exception ex)
        {
            // Skip problematic processes (like system processes you can't access)
            Console.WriteLine($"Skipping process {proc.Id}: {ex.Message}");
        }
    }
    
    return processList;
}

private string GetProcessOwner(int processId)
{
    try
    {
        using (var searcher = new ManagementObjectSearcher($"SELECT * FROM Win32_Process WHERE ProcessId = {processId}"))
        {
            foreach (ManagementObject obj in searcher.Get())
            {
                object[] ownerArgs = new object[2];
                if ((uint)obj.InvokeMethod("GetOwner", ownerArgs) == 0)
                {
                    // Return in "Domain\Username" format, same as Task Manager
                    return $"{ownerArgs[1]}\\{ownerArgs[0]}";
                }
            }
        }
    }
    catch
    {
        // Handle WMI access issues or missing processes
        return "Unknown";
    }
    
    return "Unknown";
}

Now, whenever you access a ProcessInfo object, you can directly get the UserName property without calling any extra methods—just like you do with Name or ImagePath.


These changes should get your app’s functionality and output aligned perfectly with Windows Task Manager. Just integrate these snippets into your existing code, test with different processes, and you’ll see the matching results!

内容的提问来源于stack exchange,提问作者Alejandro Gomez Guerrero

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 09:55:36