Private Sub Button1_Click(sender As Object,e As EventArgs) Handles Button1.Click For Each pro As Process In Process.GetProcesses Dim lvPro As New ListViewItem(pro.ProcessName) lvPro.SubItems.Add(pro.Id) ListView1.Items.Add(lvPro) Next End Sub
点击listview中某个进程项:
Private Sub ListView1_SelectedIndexChanged(sender As Object,e As EventArgs) Handles ListView1.SelectedIndexChanged If ListView1.SelectedItems.Count <> 1 Then Exit Sub End If Dim lvpro As New ListViewItem() lvpro = ListView1.SelectedItems(0) Dim proid As Integer = Integer.Parse(lvpro.SubItems(1).Text) Dim pro As Process = Process.GetProcessById(proid) Dim proinfo As String = "" proinfo &= "进程的名称:" & pro.ProcessName & ControlChars.CrLf proinfo &= "物理内存量:" & pro.WorkingSet64 & ControlChars.CrLf proinfo &= "最大物理内存:" & pro.PeakWorkingSet64 & ControlChars.CrLf proinfo &= "虚拟内存量:" & pro.VirtualMemorySize64 & ControlChars.CrLf proinfo &= "最大虚拟内存:" & pro.PeakVirtualMemorySize64 & ControlChars.CrLf Try proinfo &= "进程启动的时间:" & pro.StartTime.ToString("yyyy-MM-dd HH:mm:ss") & ControlChars.CrLf Catch ex As Exception proinfo &= "进程启动的时间:-" & ControlChars.CrLf End Try Try proinfo &= "总的处理器时间:" & pro.TotalProcessorTime.ToString & ControlChars.CrLf Catch ex As Exception proinfo &= "总的处理器时间:-" & ControlChars.CrLf End Try Try proinfo &= "主窗口标题:" & pro.MainWindowTitle & ControlChars.CrLf Catch ex As Exception proinfo &= "主窗口标题:-" & ControlChars.CrLf End Try TextBox1.Text = proinfo End Sub
为了防止出错,在代码中加入了try...catch语句。