遍历进程,获取进程映像路径

在VB.NET中,我们可以轻易的获取系统中正在运行的进程的名字,路径等数据。而不需要像VB6中一样使用一大堆的API函数。

下面是我某个程序中的一段代码,用来将进程名显示在一个列表框中

Sub UpdateCurrentTask() 'ok at 13-08-12 负责在独立线程实时更新正在运行的线程的变化【完成】

'负责更新显示的进程

Dim kProcess() As System.Diagnostics.Process

Dim kProc As System.Diagnostics.Process

Dim TaskList As New List(Of String)

Dim BoxList As New List(Of String)

While (True)

kProcess = System.Diagnostics.Process.GetProcesses()

TaskList.Clear()

BoxList.Clear()

For Each kProc In kProcess'将进程名保存如列表

TaskList.Add(kProc.ProcessName)

Next

For j As Integer = 0 To Running.Items.Count - 1

'将列表中的项目保存

BoxList.Add(Running.Items.Item(j))

Next'开始比对列表中现存的,如果列表中的项不存在于进程中,则会被剔除

For ji As Integer = BoxList.Count - 1 To 0 Step -1

Dim i As String = BoxList.Item(ji)

If TaskList.Contains(i) = True Then

Else

Invoke(New vRemoveRunning(AddressOf RemoveRunning), i)

BoxList.Remove(i)

End If

Next

'开始比较进程与列表,列表中不存在的进程中的项,将被加入

For ji As Integer = TaskList.Count - 1 To 0 Step -1

Dim i As String = TaskList.Item(ji)

If BoxList.Contains(i) = True Then

Else

BoxList.Add(i)

Invoke(New vAddRunning(AddressOf AddRunning), i)

End If

Next

Threading.Thread.Sleep(500)

End While

End Sub

一个简单的例子:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim kProcess() As System.Diagnostics.Process

Dim kProc As System.Diagnostics.Process

kProcess = System.Diagnostics.Process.GetProcesses()

For Each kProc In kProcess

Try

Debug.Print("进程名:" & kProc.ProcessName)

Debug.Print("映像路径:" & kProc.MainModule.FileName)

For Each pro As Diagnostics.ProcessModule In km

Debug.Print("Module Path:" & pro.FileName)

Next

Debug.Print("…………………………………………………………")

Catch ex As Exception

End Try

Next

End Sub

你可能感兴趣的:(遍历进程,获取进程映像路径)