vb.net 教程 6-2 进程 Process类属性

前端之家收集整理的这篇文章主要介绍了vb.net 教程 6-2 进程 Process类属性前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
本节内容承接上一节,主要看看如何获得某个进程的属性
窗体设计如下:

当按下“进程列表”按钮后,会在listview1中显示所有的进程及其ID,选中某个进程,将在右边文本框中显示该进程的信息。
具体代码如下:
按下按钮:
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语句。
运行时结果如图:


由于.net平台下C#和vb.NET很相似,本文也可以为C#爱好者提供参考。

学习更多vb.net知识,请参看vb.net 教程 目录

原文链接:https://www.f2er.com/vb/256071.html

猜你在找的VB相关文章