2013-04-21 132 views
0
private void UpdateProcessList() 
    { 
     // clear the existing list of any items 
     listBox1.Items.Clear(); 
     // loop through the running processes and add 
     //each to the list 
     foreach (System.Diagnostics.Process p in 
     System.Diagnostics.Process.GetProcesses()) 
     { 
      listBox1.Items.Add(p.ProcessName + " - " + p.Id); 
     } 
     // display the number of running processes in 
     // a status message at the bottom of the page 
     listBox1.Text = "Processes running: " + 
     listBox1.Items.Count.ToString(); 
    } 
    private void button1_Click(object sender, EventArgs e) 
    { 

     ProcessStartInfo pi = new ProcessStartInfo(); 
     pi.Verb = "runas"; 
     pi.FileName = "1-AccountServer.exe"; 
     pi.WindowStyle = ProcessWindowStyle.Hidden; 
     Process.Start(pi); 
     Thread.Sleep(10000); 
     pi.FileName = "2-DatabaseServer.exe"; 
     pi.WindowStyle = ProcessWindowStyle.Hidden; 
     Process.Start(pi); 
     Thread.Sleep(10000); 
     pi.FileName = "3-CoreServer.exe"; 
     pi.WindowStyle = ProcessWindowStyle.Hidden; 
     Process.Start(pi); 
     Thread.Sleep(10000); 
     pi.FileName = "4-CacheServer.exe"; 
     pi.WindowStyle = ProcessWindowStyle.Hidden; 
     Process.Start(pi); 
     Thread.Sleep(10000); 
     pi.FileName = "5-Certifier.exe"; 
     pi.WindowStyle = ProcessWindowStyle.Hidden; 
     Process.Start(pi); 
     Thread.Sleep(10000); 
     pi.FileName = "6-LoginServer.exe"; 
     pi.WindowStyle = ProcessWindowStyle.Hidden; 
     Process.Start(pi); 
     Thread.Sleep(10000); 
     pi.FileName = "7-WorldServer.exe"; 
     pi.WindowStyle = ProcessWindowStyle.Hidden; 
     Process.Start(pi); 
     Thread.Sleep(10000); 

    } 

问题是我不希望每个进程都显示,只是这七个特定的进程,我该怎么做?它确实有效,但不是我真正想要的方式>。 <我一直在互联网上寻找一段时间来真正找到这个代码,并实现它到我想要的,但它只是显示了这么多的过程,我想要它是毫无意义的。仅显示某些过程?

+1

限制过程更好的建议通过IV4你需要先将它们添加到一个集合,比如一个列表,然后使用linq查询来过滤 – cgatian 2013-04-21 16:12:29

回答

1

与您的所有进程名

List<string> myProcesses = new List<string>() 
{ 
    "1-AccountServer.exe","2-DatabaseServer.exe", 
    "3-CoreServer.exe", "4-CacheServer.exe","5-Certifier.exe", 
    "6-LoginServer.exe","7-WorldServer.exe" 
}; 

和创造(的字符串)的列表,然后通过该方式检查

foreach (Process p in Process.GetProcesses()) 
{ 
    if(myProcesses.Contains(p.ProcessName + ".exe")) 
     listBox1.Items.Add(p.ProcessName + " - " + p.Id); 
} 

,创建这个列表可以帮助很多也共同打造的方法启动的所有进程

private void button1_Click(object sender, EventArgs e) 
{ 
    ProcessStartInfo pi = new ProcessStartInfo(); 
    pi.Verb = "runas"; 
    pi.WindowStyle = ProcessWindowStyle.Hidden; 
    foreach(string s in myProcesses) 
    { 
     pi.FileName = s; 
     Process.Start(pi); 
     Thread.Sleep(10000); 
    } 
} 

编辑在其评论,也许不同的方法,收集在一个HashSet的所有进程,然后检查列表(仅7元)对HashSet的可能是在性能方面

HashSet<Process> anHashOfProcesses = new HashSet<Process>(Process.GetProcesses()); 
foreach(string s in myProcesses) 
{ 
    var p = anHashOfProcesses.FirstOrDefault(z => z.ProcessName + ".exe" == s); 
    if(p != null) listBox1.Items.Add(p.ProcessName + " - " + p.Id); 
} 
+0

我将使用'HashSet'而不是列表。 – I4V 2013-04-21 16:16:02

+0

非常感谢你的赞赏:) – 2013-04-21 16:18:35

+0

GAH我不能让它工作,但我想这是因为它检查过程之前,他们开始,而不是他们已经开始后?这些进程实际上是开始的,但它们仍然不显示在列表框中? 我需要显示我的所有代码>。但我不知道如何:( – 2013-04-21 16:42:39