private void ProcessesMenuKillProcess_Click(object sender, EventArgs e) { string name = listViewProc.SelectedItems[0].SubItems[0].Text; GetProcess.KillProcess(name.Remove(name.Length - 4)); ListView2(); }
public string[,] GetAllProcesses() { string wmiClass = "Win32_Process"; string condition = ""; string[] queryProperties = new string[] { "Name", "ProcessId", "Caption", "ExecutablePath", "CommandLine", "ParentProcessId" }; SelectQuery wmiQuery = new SelectQuery(wmiClass, condition, queryProperties); ManagementScope scope = new System.Management.ManagementScope(@"\\.\root\CIMV2"); ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, wmiQuery); ManagementObjectCollection runningProcesses = searcher.Get(); Process[] prs = Process.GetProcesses(); GetProcess processes = new GetProcess(); int count = 0; string[,] str = new string[runningProcesses.Count, 5]; foreach (ManagementObject obj in runningProcesses) { if (obj["Caption"] != null) { str[count, 0] = obj["Name"].ToString(); } else { str[count, 0] = "---"; } if (obj["ExecutablePath"] != null) { str[count, 1] = obj["ExecutablePath"].ToString(); } else { str[count, 1] = "---"; } if (obj["ProcessId"] != null) { str[count, 2] = obj["ProcessId"].ToString(); } else { str[count, 2] = "---"; } str[count, 3] = obj["ParentProcessId"].ToString(); for (int i = 0; i < prs.Length; i++) { if (prs[i].MainWindowTitle.Length > 0 && obj["Name"].ToString() == prs[i].ProcessName.ToString() + ".exe") { str[count, 4] = prs[i].MainWindowTitle; break; } } count++; } return(str); }
private void ListView2() { listViewProc.Items.Clear(); GetProcess processes = new GetProcess(); string[,] SProcess = processes.GetAllProcesses(); for (int row = 0; row < SProcess.GetLength(0); row++) { ListViewItem newItem = new ListViewItem(SProcess[row, 0]); newItem.SubItems.Add(SProcess[row, 1]); newItem.SubItems.Add(SProcess[row, 2]); for (int p = 0; p < SProcess.GetLength(0); p++) { if (SProcess[row, 3] == SProcess[p, 2]) { newItem.SubItems.Add(SProcess[p, 0]); break; } else if (p == SProcess.GetLength(0) - 1) { newItem.SubItems.Add(SProcess[row, 3]); } } newItem.SubItems.Add(SProcess[row, 4]); listViewProc.Items.Add(newItem); }/* * int i = 0; * foreach (Process process in Process.GetProcesses()) * { * ListViewItem newItem = new ListViewItem(process.ProcessName.ToString()); * newItem.SubItems.Add(process.Id.ToString()); * // newItem.SubItems.Add(SProcess[i, 3]); * newItem.SubItems.Add(i.ToString()); * // MessageBox.Show(GetProcess.GetParentProcess(process.Id).ToString()); * // newItem.SubItems.Add( * // GetProcess.GetProcessOwner(process.ProcessName.ToString())); * listViewProc.Items.Add(newItem); * i++; * }*/ int col1 = 2, col2 = 3, col3 = 2, col4 = 2, col5 = 2; double scroll = ((double)100 / 693) * 16 / 5; float coef = listViewProc.Width / (col1 + col2 + col3 + col4 + col5); int width = Convert.ToInt32(scroll / 100 * listViewProc.Width) + 1; listViewProc.Columns[0].Width = Convert.ToInt32(col1 * coef) - width; listViewProc.Columns[1].Width = Convert.ToInt32(col2 * coef) - width; listViewProc.Columns[2].Width = Convert.ToInt32(col3 * coef) - width; listViewProc.Columns[3].Width = Convert.ToInt32(col4 * coef) - width; listViewProc.Columns[4].Width = Convert.ToInt32(col5 * coef) - width - 1; }
private void Form1_Load(object sender, EventArgs e) { startWatch = new ManagementEventWatcher( new WqlEventQuery("SELECT * FROM Win32_ProcessStartTrace")); startWatch.EventArrived += new EventArrivedEventHandler(startWatch_EventArrived); startWatch.Start(); stopWatch = new ManagementEventWatcher( new WqlEventQuery("SELECT * FROM Win32_ProcessStopTrace")); stopWatch.EventArrived += new EventArrivedEventHandler(stopWatch_EventArrived); stopWatch.Start(); MenuStripLanguageBulgarian.Enabled = false;//default language is BG MenuStripLanguageEnglish.Enabled = true; res_man = new ResourceManager("Optimizer.Resource.Res", typeof(Form1).Assembly); switch_language(); string Today = DateTime.Now.ToShortDateString(); List <string> DisplayName; List <string> DisplayName64; List <string> Location; List <string> Location64; InstalledPrograms.GetInstalledProgramsFromRegistry(RegistryView.Registry32, out DisplayName, out Location); InstalledPrograms.GetInstalledProgramsFromRegistry(RegistryView.Registry64, out DisplayName64, out Location64); DisplayName.AddRange(DisplayName64); AllDisplayName = DisplayName; Location.AddRange(Location64); AllLocation = Location; newDoc = new CreateExcelDoc("Table.xls");//Create excel doc ExcelMatrix = newDoc.ReadMatrix(); if (ExcelMatrix.GetLength(0) <= 1) { newDoc.AddArrayInCol(0, 1, DisplayName.ToArray()); newDoc.NewDate(Today, DisplayName.Count + 1); ExcelMatrix = newDoc.ReadMatrix(); } else { if (ExcelMatrix.GetLength(0) == DisplayName.Count + 1) { newDoc.NewDate(Today, DisplayName.Count + 1); } } string[,] newExcelMatrix = new string[AllDisplayName.Count() + 1, 8]; Array.Copy(ExcelMatrix, newExcelMatrix, 8); for (int i = 0; i < AllDisplayName.Count(); i++) { newExcelMatrix[i + 1, 0] = AllDisplayName[i]; for (int p = 0; p < ExcelMatrix.GetLength(0); p++) { if (newExcelMatrix[i + 1, 0] == ExcelMatrix[p, 0]) { newExcelMatrix[i + 1, 1] = ExcelMatrix[p, 1]; newExcelMatrix[i + 1, 2] = ExcelMatrix[p, 2]; newExcelMatrix[i + 1, 3] = ExcelMatrix[p, 3]; newExcelMatrix[i + 1, 4] = ExcelMatrix[p, 4]; newExcelMatrix[i + 1, 5] = ExcelMatrix[p, 5]; newExcelMatrix[i + 1, 6] = ExcelMatrix[p, 6]; newExcelMatrix[i + 1, 7] = ExcelMatrix[p, 7]; break; } else if (p == (ExcelMatrix.GetLength(0) - 1)) { newExcelMatrix[i + 1, 1] = "0"; newExcelMatrix[i + 1, 2] = "0"; newExcelMatrix[i + 1, 3] = "0"; newExcelMatrix[i + 1, 4] = "0"; newExcelMatrix[i + 1, 5] = "0"; newExcelMatrix[i + 1, 6] = "0"; newExcelMatrix[i + 1, 7] = "0"; } } } ExcelMatrix = newExcelMatrix; process = new GetProcess(); int ProcesCount = process.GetAllProcesses().GetLength(0); SProcess = process.GetAllProcesses(); ListView1(); ListView2(); ListView3(ExcelMatrix); ListView4(); }