Get Running Process C#

If you want to access local computer process this might get you started! In this example i used a windows form with a  listview and imagelist. Nothing fancy just a simple application that might help you started with System.Diagnostics.Process check Microsoft's documentation click here


Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace GetRunningProcess
{
    public partial class frmGetProcess : Form
    {
        public frmGetProcess()
        {
            InitializeComponent();
        }

        private void SetupListView()
        {
            this.listView1.Items.Clear();
            this.listView1.View = View.Tile ;
            this.listView1.LargeImageList = this.imageList1;
            this.listView1.Columns.Add("Image Name", 100, HorizontalAlignment.Left);
        
        }

        private void frmGetProcess_Load(object sender, EventArgs e)
        {
            SetupListView();

            this.listView1.BeginUpdate();

            Process[] proc = Process.GetProcesses();

            foreach (Process proc_ in proc)
            {
                ListViewItem item = new ListViewItem(proc_.ProcessName);
             
                this.listView1.Items.Add(item);
                item.ImageIndex = 0;
            }

            this.listView1.EndUpdate();
        }
    }
}


Form1.Designer.cs
namespace GetRunningProcess
{
    partial class frmGetProcess
    {
        /// 
        /// Required designer variable.
        /// 
        private System.ComponentModel.IContainer components = null;

        /// 
        /// Clean up any resources being used.
        /// 
        /// true if managed resources should be disposed; otherwise, false.        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// 
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// 
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(frmGetProcess));
            this.listView1 = new System.Windows.Forms.ListView();
            this.imageList1 = new System.Windows.Forms.ImageList(this.components);
            this.SuspendLayout();
            // 
            // listView1
            // 
            this.listView1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.listView1.Location = new System.Drawing.Point(0, 0);
            this.listView1.Name = "listView1";
            this.listView1.Size = new System.Drawing.Size(394, 415);
            this.listView1.TabIndex = 0;
            this.listView1.UseCompatibleStateImageBehavior = false;
            // 
            // imageList1
            // 
            this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
            this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
            this.imageList1.Images.SetKeyName(0, "icon_process.ico");
            // 
            // frmGetProcess
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(394, 415);
            this.Controls.Add(this.listView1);
            this.Name = "frmGetProcess";
            this.Text = "Get Running Process";
            this.Load += new System.EventHandler(this.frmGetProcess_Load);
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.ListView listView1;
        private System.Windows.Forms.ImageList imageList1;
    }
}



Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace GetRunningProcess
{
    static class Program
    {
        /// 
        /// The main entry point for the application.
        /// 
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new frmGetProcess());
        }
    }
}

2 comments:

Anonymous said...
This comment has been removed by a blog administrator.
Pravesh Singh said...

Very nice article. I really enjoyed it reading. And it also cleared lot of my doubts about running process in c#. Check this link too its also having nice post with wonderful explanation on Checking process in C#.

http://www.mindstick.com/Articles/c5c08a18-908c-4410-925b-a4f57e62e1c7/?RUNNING%20PROCESS%20IN%20C#

Thanks Everyone for your precious post.

Post a Comment