[英]C# How to add new processes to a list of processes using a checkedlistbox
我正在為一個清理實用程序編寫一個 windows 窗體應用程序,其中 windows 窗體應用程序將執行多個具有相同進程屬性的批處理文件來清理計算機的各個部分,這就是我到目前為止所擁有的,
ProcessStartInfo[] infos = new ProcessStartInfo[]
{
new ProcessStartInfo(Environment.CurrentDirectory + @"example batch file 1"),
new ProcessStartInfo(Environment.CurrentDirectory + @"example batch file 2"),
};
然后我執行它們,
Process[] startedProcesses = StartProcesses(infos, true);
每個進程的屬性都包含在,
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();
}
}
}
問題是,我想使用 if 語句將新的批處理文件添加到列表中,因為我希望用戶使用檢查列表框控制執行哪些批處理文件,例如,
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"),
}
};
但這不起作用......反正有這個嗎?
親切的問候,雅各布
在您的上一個代碼片段中,您有語法錯誤,因為它不是填充數組的正確方法。 我修改了它,所以它是一個簡單的例子並且使用了一個列表。 它根據選中的項目啟動應用程序。 你應該准確地顯示你有什么錯誤。
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);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.