Mega Code Archive

 
Categories / VB.Net Tutorial / Development
 

List all process in a ListView

Imports System.Diagnostics Imports System.Windows.Forms public class ProcessManager    public Shared Sub Main         Application.Run(New Form1)    End Sub End class Public Class Form1     Private Sub ListProcesses()         Dim ps() As Process         Try             ps = Process.GetProcesses()             lvProcesses.BeginUpdate()             lvProcesses.Clear()             lvProcesses.Columns.Add("Name", 100, HorizontalAlignment.Left)             lvProcesses.Columns.Add("ID", 60, HorizontalAlignment.Left)             lvProcesses.Columns.Add("Priority", 60, HorizontalAlignment.Right)             lvProcesses.Columns.Add("Memory", 100, HorizontalAlignment.Right)             Dim p As Process             For Each p In ps                 Dim lvi As ListViewItem = New ListViewItem()                 lvi.Text = p.ProcessName                 lvi.SubItems.Add(p.Id.ToString())                 lvi.SubItems.Add(p.BasePriority.ToString())                 lvi.SubItems.Add(p.WorkingSet64.ToString())                 lvProcesses.Items.Add(lvi)             Next p             lvProcesses.EndUpdate()         Catch e As Exception             MessageBox.Show(e.Message)         End Try     End Sub     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load         ListProcesses()     End Sub     Private Sub btnKillProcess_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnKillProcess.Click         If MessageBox.Show(Me, "Stop Process" + lvProcesses.SelectedItems(0).Text, "Stop", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) = DialogResult.Cancel Then             Return         End If         Dim pid As Integer = Int32.Parse(lvProcesses.SelectedItems(0).SubItems(1).Text)         Dim p As Process = Process.GetProcessById(pid)         If p Is Nothing Then Return         If Not p.CloseMainWindow() Then p.Kill()         p.WaitForExit()         p.Close()         ListProcesses()     End Sub     Private Sub btnRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRefresh.Click         ListProcesses()     End Sub End Class <Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _ Partial Class Form1     Inherits System.Windows.Forms.Form     <System.Diagnostics.DebuggerNonUserCode()> _     Protected Overrides Sub Dispose(ByVal disposing As Boolean)         If disposing AndAlso components IsNot Nothing Then             components.Dispose()         End If         MyBase.Dispose(disposing)     End Sub     Private components As System.ComponentModel.IContainer     <System.Diagnostics.DebuggerStepThrough()> _     Private Sub InitializeComponent()         Me.btnRefresh = New System.Windows.Forms.Button         Me.label1 = New System.Windows.Forms.Label         Me.btnKillProcess = New System.Windows.Forms.Button         Me.btnNewProcess = New System.Windows.Forms.Button         Me.lvProcesses = New System.Windows.Forms.ListView         Me.btnProcessProp = New System.Windows.Forms.Button         Me.SuspendLayout()         '         'btnRefresh         '         Me.btnRefresh.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft         Me.btnRefresh.Location = New System.Drawing.Point(557, 11)         Me.btnRefresh.Margin = New System.Windows.Forms.Padding(4)         Me.btnRefresh.Name = "btnRefresh"         Me.btnRefresh.Size = New System.Drawing.Size(116, 35)         Me.btnRefresh.TabIndex = 11         Me.btnRefresh.Text = "Refresh(&R)"         Me.btnRefresh.UseVisualStyleBackColor = True         '         'label1         '         Me.label1.AutoSize = True         Me.label1.Location = New System.Drawing.Point(13, 24)         Me.label1.Margin = New System.Windows.Forms.Padding(4, 0, 4, 0)         Me.label1.Name = "label1"         Me.label1.Size = New System.Drawing.Size(112, 15)         Me.label1.TabIndex = 10         Me.label1.Text = "Processes"         '         'btnKillProcess         '         Me.btnKillProcess.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft         Me.btnKillProcess.Location = New System.Drawing.Point(509, 347)         Me.btnKillProcess.Margin = New System.Windows.Forms.Padding(4)         Me.btnKillProcess.Name = "btnKillProcess"         Me.btnKillProcess.Size = New System.Drawing.Size(164, 29)         Me.btnKillProcess.TabIndex = 9         Me.btnKillProcess.Text = "Stop(&K)"         Me.btnKillProcess.UseVisualStyleBackColor = True         '         'btnNewProcess         '         Me.btnNewProcess.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft         Me.btnNewProcess.Location = New System.Drawing.Point(362, 347)         Me.btnNewProcess.Margin = New System.Windows.Forms.Padding(4)         Me.btnNewProcess.Name = "btnNewProcess"         Me.btnNewProcess.Size = New System.Drawing.Size(139, 29)         Me.btnNewProcess.TabIndex = 8         Me.btnNewProcess.Text = "New(&N)"         Me.btnNewProcess.UseVisualStyleBackColor = True         '         'lvProcesses         '         Me.lvProcesses.Location = New System.Drawing.Point(13, 54)         Me.lvProcesses.Margin = New System.Windows.Forms.Padding(4)         Me.lvProcesses.MultiSelect = False         Me.lvProcesses.Name = "lvProcesses"         Me.lvProcesses.Size = New System.Drawing.Size(659, 285)         Me.lvProcesses.TabIndex = 7         Me.lvProcesses.UseCompatibleStateImageBehavior = False         Me.lvProcesses.View = System.Windows.Forms.View.Details         '         'btnProcessProp         '         Me.btnProcessProp.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft         Me.btnProcessProp.Location = New System.Drawing.Point(32, 347)         Me.btnProcessProp.Margin = New System.Windows.Forms.Padding(4)         Me.btnProcessProp.Name = "btnProcessProp"         Me.btnProcessProp.Size = New System.Drawing.Size(153, 29)         Me.btnProcessProp.TabIndex = 6         Me.btnProcessProp.Text = "Properties(&P)"         Me.btnProcessProp.UseVisualStyleBackColor = True         '         'Form1         '         Me.AutoScaleDimensions = New System.Drawing.SizeF(8.0!, 15.0!)         Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font         Me.ClientSize = New System.Drawing.Size(679, 395)         Me.Controls.Add(Me.btnRefresh)         Me.Controls.Add(Me.label1)         Me.Controls.Add(Me.btnKillProcess)         Me.Controls.Add(Me.btnNewProcess)         Me.Controls.Add(Me.lvProcesses)         Me.Controls.Add(Me.btnProcessProp)         Me.ResumeLayout(False)         Me.PerformLayout()     End Sub     Private WithEvents btnRefresh As System.Windows.Forms.Button     Private WithEvents label1 As System.Windows.Forms.Label     Private WithEvents btnKillProcess As System.Windows.Forms.Button     Private WithEvents btnNewProcess As System.Windows.Forms.Button     Private WithEvents lvProcesses As System.Windows.Forms.ListView     Private WithEvents btnProcessProp As System.Windows.Forms.Button End Class