private void LoadAllProgramToList() { //清空进程imageList和ProcessView processImageList.Images.Clear(); ProcessView.Items.Clear(); foreach (int i in GlobalVariable.Started_PID) { ListViewItem lvi = new ListViewItem(); lvi.Text = Convert.ToString(i); //获取对应PID的启动目录 Process pro; string proPathName, proArgument; try { pro = Process.GetProcessById(i);//获得对应PID的进程 proPathName = pro.MainModule.FileName.ToString(); proArgument = ProcessExtensions.GetCommandLineArgs(pro); } catch { continue; } string[] args = CommandLineExtensions.ConvertCommandLineToArgs(proArgument); string sumArg = ""; for (int a = 1; a < args.Length; a++) { sumArg += args[a] + " "; } lvi.SubItems.Add(proPathName); lvi.SubItems.Add(sumArg); if (GlobalVariable.Hid_PID.Contains(i)) { lvi.SubItems.Add("隐藏"); } else { lvi.SubItems.Add("正常"); } processImageList.Images.Add(Convert.ToString(i), IconHelper.GetFileIcon(proPathName, true)); lvi.ImageIndex = processImageList.Images.Count - 1; //通过与imageList绑定,显示imageList中第i项图标 ProcessView.Items.Add(lvi); } //更新新的Started_PID表 GlobalVariable.Started_PID.Clear(); foreach (ListViewItem i in ProcessView.Items) { GlobalVariable.Started_PID.Add(Convert.ToInt32(i.Text)); } }
public void Run() { try { processSelectWindow.Invoke((EventHandler) delegate { //清空进程imageList和ProcessView processImageList.Images.Clear(); processView.Items.Clear(); loadProgressBar.Value = 0; statusLabel.Text = "正在加载进程列表……"; }); //获取所有进程 Process[] pro = Process.GetProcesses(); int max_count = pro.Count(); int progress = 0; processSelectWindow.Invoke((EventHandler) delegate { loadProgressBar.Maximum = max_count; }); foreach (Process item in pro) { progress += 1; ListViewItem lvi = new ListViewItem { Text = item.ProcessName }; //获取对应PID的启动目录 string proPathName, proArgument; proPathName = GetProcessFilename(item); proArgument = ProcessExtensions.GetCommandLineArgs(item); string[] args = CommandLineExtensions.ConvertCommandLineToArgs(proArgument); string sumArg = ""; for (int a = 1; a < args.Length; a++) { sumArg += args[a] + " "; } lvi.SubItems.Add(Convert.ToString(item.Id)); lvi.SubItems.Add(proPathName); lvi.SubItems.Add(sumArg); processSelectWindow.Invoke((EventHandler) delegate { processImageList.Images.Add(Convert.ToString(item.Id), IconHelper.GetFileIcon(proPathName, true)); lvi.ImageIndex = processImageList.Images.Count - 1; //通过与imageList绑定,显示imageList中第i项图标 processView.Items.Add(lvi); statusLabel.Text = $@"正在加载进程列表……({progress}/{max_count})"; loadProgressBar.Value = progress; }); } processSelectWindow.Invoke((EventHandler) delegate { statusLabel.Text = $@"进程列表加载完毕,共加载了{progress}个进程!"; }); } catch { } }