【问题标题】:Search specific letters in process name在进程名称中搜索特定字母
【发布时间】:2019-07-14 08:52:57
【问题描述】:

我在搜索过程中遇到问题。我想用 c# 在所有正在运行的进程中搜索特定的字母。例如我的进程有这个进程名称(notepad,notepad++,note,calc,mspaint),我想搜索“note”,结果搜索必须是三个项目(notepad,notepad++,note),因为只有三个进程包含“note”。如何编程...

此代码仅找到“记事本”,未找到包含字母

Process[] pname = Process.GetProcessesByName("notepad");
                if (pname.Length == 0)
                {

                }
                else
                {
                    //some code if process found
                }

【问题讨论】:

标签: c# .net


【解决方案1】:

你可以这样做

    static void Main(string[] args)
    {
        Process.GetProcesses() //get all process 
                     .Where(x => x.ProcessName.ToLower() // lower their names to lower cases
                                  .Contains("note")) //where their names contain note
                     .ToList() //convert to list
                     .ForEach(DoSomethingWithResults); //iterate over the items
    }

    private static void DoSomethingWithResults(Process obj)
    {
        //Do Something With Results
    }
【解决方案2】:

首先,遍历所有过程,然后使用Contains 方法或Equals 反匹配您想要的字符串/短语。

例子:

Process[] processlist = Process.GetProcesses();

foreach (Process theprocess in processlist)
{
 if (theprocess.ProcessName.Contains("note")
  {

     ///Do your work here

  }
}

【讨论】:

    【解决方案3】:

    另一种方法是

    string searchText = "note";
    List<Process> pfilteredProcess = Process.GetProcesses() // Get All Process
                .Where(p => p.ProcessName.ToLower() // Lower the name case of Process
                .Contains(searchText.ToLower()))   // Lower name of Search text
                .ToList();
    //Work on the Searched List
            foreach (Process process in pfilteredProcess)
            {
                ///Do Activity
            }
    

    【讨论】:

      猜你喜欢
      • 2017-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-08
      • 2014-04-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多