listView1.Items.Clear(); Process proc = new Process(); ProcessStartInfo procStartInfo; string command = "/c" + "Powershell Get-VM | findstr /R Running "; procStartInfo = new ProcessStartInfo("CMD", command); procStartInfo.RedirectStandardOutput = true; procStartInfo.UseShellExecute = false; procStartInfo.CreateNoWindow = true; //Process proc = new Process(); proc.StartInfo = procStartInfo; proc.StartInfo = procStartInfo; proc.OutputDataReceived += new DataReceivedEventHandler(proc_OutputDataReceived1); proc.Start(); proc.BeginOutputReadLine(); procStartInfo.WindowStyle = ProcessWindowStyle.Hidden; proc.WaitForExit(); if (k == 0) { listView1.Items.Add("No Virtual Machine is running"); } } catch (Exception e) { } } void proc_OutputDataReceived1(object sender, DataReceivedEventArgs e) { try { if (e.Data != null) { k++; string str = e.Data.Trim() + Environment.NewLine; if (str.Contains("Running")) { int index = str.IndexOf("Running"); string result = str.Substring(0, index); if (virtualmachnames.Contains(result)) { } else { virtualmachnames.Add(result); } ListViewItem newList = new ListViewItem(result); newList.SubItems.Add("Running"); MethodInvoker append = () => listView1.Items.Add(newList); FileIO.LogNotify("MAIN", "checkvirtual", FileIO.NotifyType.INFO, "Virtual machine runnning are:", result); listView1.BeginInvoke(append); } } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)