0

I have a problem in search process . I want to search specific letter in all running process with c#. for example my process have this process name(notepad,notepad++,note,calc,mspaint) and I want to search "note" ,Result search must be three item (notepad,notepad++,note) because only three process contain "note" . How programing ...

this code found only "notepad" and not found contain letter

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

                }
                else
                {
                    //some code if process found
                }
slugster
  • 49,403
  • 14
  • 95
  • 145
nace
  • 61
  • 1
  • 6

3 Answers3

1

You can do something like this

    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
    }
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
styx
  • 1,852
  • 1
  • 11
  • 22
  • 1
    [Why avoid string.ToLower() when doing case-insensitive string comparisons?](https://stackoverflow.com/q/28440783/1364007) – Wai Ha Lee Jul 14 '19 at 17:37
0

First, loop through all the proccesses and then counter-match your desired string/phrase, either with Contains method or Equals.

Example :

Process[] processlist = Process.GetProcesses();

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

     ///Do your work here

  }
}
Software Dev
  • 5,368
  • 5
  • 22
  • 45
0

Another way of doing it would be

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
        }
Praadeep
  • 79
  • 4