Q:

C# How to add new processes to a list of processes using a checkedlistbox

I am writing a windows forms application for a cleanup utility in which the windows forms application will execute multiple batch files with the same process attributes to cleanup various sections of the computer, this is what I have so far,
ProcessStartInfo[] infos = new ProcessStartInfo[]
{
    new ProcessStartInfo(Environment.CurrentDirectory + @"example batch file 1"),
    new ProcessStartInfo(Environment.CurrentDirectory + @"example batch file 2"),
};

I then execute them with,
Process[] startedProcesses = StartProcesses(infos, true);

The attributes for every process are contained within,
public Process[] StartProcesses(ProcessStartInfo[] infos, bool waitForExit)
    {
        ArrayList processesBuffer = new ArrayList();
        foreach (ProcessStartInfo info in infos)
        {
            Process process = Process.Start(info);

            if (waitForExit)
            {
                process.StartInfo.UseShellExecute = true;
                process.StartInfo.Verb = "runas";
                process.WaitForExit();
            }
        }
    }

The issue is, I want to add new batch files to the list with if statements as I want the user to control what batch files are executed using a checkedlistbox eg,
ProcessStartInfo[] infos = new ProcessStartInfo[]
{
    if (checkedListBox1.GetItemCheckState(0) == CheckState.Checked)
        {
            new ProcessStartInfo(Environment.CurrentDirectory + @"example batch file 1"),
        }
    if (checkedListBox1.GetItemCheckState(1) == CheckState.Checked)
        {
            new ProcessStartInfo(Environment.CurrentDirectory + @"example batch file 2"),
        }
};

But this does not work... Is there anyway around this?
Kind regards, Jacob

A:

In your last code fragment, you had syntax errors, because it's not a proper way to fill the array. I modified it, so it is a simple example and uses a List. It starts apps based on checked items. And you should show exactly what errors did you have.
private void button1_Click(object sender, EventArgs e)
    {
        List<ProcessStartInfo> startInfos = new List<ProcessStartInfo>();

        if (checkedListBox1.GetItemChecked(0))
        {
            startInfos.Add(new ProcessStartInfo("notepad.exe"));
        }
        if (checkedListBox1.GetItemChecked(1))
        {
            startInfos.Add(new ProcessStartInfo("calc.exe"));
        }
        if (checkedListBox1.GetItemChecked(2))
        {
            startInfos.Add(new ProcessStartInfo("explorer.exe"));
        }

        foreach (var startInfo in startInfos)
        {
            Process.Start(startInfo);
        }
    }

