Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
C# 2005 For discussion of Visual C# 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old January 26th, 2008, 09:05 PM
Friend of Wrox
Join Date: Jun 2005
Posts: 101
Thanks: 0
Thanked 1 Time in 1 Post
Default Getting the Process ID

How do I find the Process ID of a program? I need to know it to attach a process for WinDbg. And, yes, I tried googling this question but everything seemed to be for a Linux system.

Old January 26th, 2008, 10:42 PM
Friend of Wrox
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts

Here's a couple chunks of code from a little remoting experiment I was running. TestServer runs on IIS in a remote process, and the client runs in a local process.

private void button1_Click(object sender, System.EventArgs e) {
            System.Text.StringBuilder output = new System.Text.StringBuilder();
            TestServer.TestService test = new TestServer.TestService();

            output.AppendFormat("TestServer values\n");
            output.AppendFormat("Machine: {0}\n", test.GetServerName());
            output.AppendFormat("Process: {0}\n", test.GetProcessID());

            output.AppendFormat("\nClient values\n");
            output.AppendFormat("Machine: {0}\n", System.Environment.MachineName);
            output.AppendFormat("Process: {0}\n", System.Diagnostics.Process.GetCurrentProcess().Id);

            MessageBox.Show(output.ToString(), "TestClient");


public class TestService : MarshalByRefObject
        public TestService()    {}

        public string GetServerName() {
            return System.Environment.MachineName;

        public int GetProcessID(){
            return System.Diagnostics.Process.GetCurrentProcess().Id;



Old January 28th, 2008, 02:22 AM
Imar's Avatar
Wrox Author
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts

Hi Bill,

If you are able / willing to use C# 2008, you can use LINQ to get the IDs like this:
var processIds = from p in System.Diagnostics.Process.GetProcesses()
                 where p.ProcessName.Contains("devenv")
                 select p.Id;

foreach (var processId in processIds)
    Debug.WriteLine(String.Format("Process Id {0}", processId.ToString()));
The example use the where clause to limit the processes by their name but other criteria are possible as well. This gives you an IEnumerable<int> that you can loop through as the code demonstrates.

Alternatively, and less cool (but works in C# 2005), use GetProcessesByName.
Process[] processes = System.Diagnostics.Process.GetProcessesByName("devenv");
Both methods could result in multiple processes / IDs being returned, so if your application allows multiple instances, you need to take care of that as somehow.

Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
Old January 28th, 2008, 04:05 AM
joefawcett's Avatar
Wrox Author
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts

If you just want to know then you can open the task manager, Ctrl+Shift+Esc, and then choose the processes tab. If the PID column isn't there then choose View > Select columns and add it in. Alternatively open a command window and tyep tasklist and press enter.


Joe (Microsoft MVP - XML)

Similar Threads
Thread Thread Starter Forum Replies Last Post
Transaction (Process ID 59) was deadlocked aksvls VS.NET 2002/2003 4 April 2nd, 2011 03:35 PM
process.startinfo opens new instance of process Anypond General .NET 0 August 28th, 2008 05:35 AM
creating process angelboy C# 2005 4 June 20th, 2007 07:00 PM
'this.ID = id;' in class construction holf BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 0 October 6th, 2006 10:58 AM
why not index.asp?id=1 can be www.myweb.com/?id=1 BurhanKhan Classic ASP Professional 11 September 6th, 2004 02:06 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.