我在搜索过程中遇到问题。我想在所有使用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
}
答案 0 :(得分:0)
首先,循环遍历所有过程,然后使用Contains
方法或Equals
对匹配您想要的字符串/短语。
示例:
Process[] processlist = Process.GetProcesses();
foreach (Process theprocess in processlist)
{
if (theprocess.ProcessName.Contains("note")
{
///Do your work here
}
}
答案 1 :(得分:0)
您可以这样做
static void Main(string[] args)
{
Process.GetProcesses() //get all process
.Where(x => x.ProcessName.ToLower() // lower thier names to lower cases
.Contains("note")) //where thier names conain note
.ToList() //convert to list
.ForEach(DoSomethingWithResults); //itterate over the items
}
private static void DoSomethingWithResults(Process obj)
{
//Do Something With Results
}
答案 2 :(得分:0)
另一种方法是
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
}