How to get process id of Console in C# console app

7.7k views Asked by At

In my hello world console app, Process.GetCurrentProcess().Id property returns a different value from the Id for the Console window created to display the app's stdout etc.

How do I get the process id for the console window specifically?

I cycle through the processes in Process.GetProcesses() and check for the console window based on window title. When it finds it, I print it's process id out and it's different to what is returned from the GetCurrentProcess() call. So I'm concluding the console app process and the console window are two different processes, maybe the console window is a child process of my console app, or maybe it's a peculiarity associated with running a console app from within Visual Studio.

        Process[] processlist = Process.GetProcesses();
        int origProcessId = Process.GetCurrentProcess().Id;
        foreach ( Process p in processlist)
        {
            // get all window handles of title 'C:\Windows\system32\cmd.exe
            if (!String.IsNullOrEmpty(p.MainWindowTitle) && p.MainWindowTitle.IndexOf("C:\\Windows\\system32\\cmd.exe") == 0 )
            {
                    Console.WriteLine("Gets here ok, once & only once");
                    if(origProcessId == p.Id){
                                Console.WriteLine("Process: {0}", p.Id); // doesn't get here!!!
                    }
            }
        }
1

There are 1 answers

0
vesan On BEST ANSWER

I think it would be useful for us to know why you need the process id. The problem is that there are multiple ways that your application can be launched and every one of them will look a little differently:

In Visual Studio, Run with debugging:

This will make you application run in a single process. The MainWindowTitle will look similar to the following:

file://C:\...\ConsoleApplication.exe

In Visual Studio, Run without debugging:

This will start cmd.exe and have that start your application. So, your application will be a separate process from cmd.exe and will have no MainWindowTitle (because it has no window). You can see the process running as a child of cmd.exe in Process Explorer:

enter image description here

Without Visual Studio:

When double-clicking the exe of your application, you'll get a single process whose MainWindowTitle will be the path to your exe (so the same thing as the first case but without the file://). You can also get it running like this when debugging with VS if you uncheck "Enable the Visual Studio hosting process" in your project's Debug options.

Without Visual Studio, using command line

This will give you exactly the same result as VS's "Run without debugging" option.

I think the important message here is: don't use MainWindowTitle to find your application. Process.GetCurrentProcess() will always give you the current process id.

If, for some reason, you want to find the parent process, I suggest looking at this question. I think you should clarify: why do you need to find the process id? What do you want to do with it?