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

    }

问题是我不希望每个流程都显示,而只是这七个特定流程,我该怎么做?它确实有效,但不是我真正想要的方式>。<我在互联网上搜索了一段时间以实际找到这段代码并将其实现为我想要的,但它只是显示了这么多我想要的过程因为是没有意义的。

4

1 回答 1

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

按照 IV4 在其评论中的建议进行编辑,也许一种不同的方法将所有进程收集在 HashSet 中,然后针对 HashSet 检查列表(只有 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);
}
于 2013-04-21T16:14:26.647 回答