只显示某些流程?

时间:2013-04-21 16:08:30

标签: c# process

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 个答案:

答案 0 :(得分:1)

创建一个包含所有进程名称的List(of String)

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);
    }
 }
<4> EDIT 正如IV4在其评论中所建议的那样,也许是一种不同的方法,它收集HashSet中的所有进程,然后针对HashSet检查List(只有7个元素)可能更好性能

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);
}