locked
Get Process ID from Window Title RRS feed

  • Question

  • I searched everywhere and can not find anything useful on this topic. Is this even possible?

    I have multiple process. Each have the same process name, but different window titles. 

    I want to use the window title to find out the process ID. 

    Any suggestions? Thank you.

    Wednesday, January 9, 2013 11:14 AM

Answers

  • var processes = Process.GetProcesses().Where(p => !string.IsNullOrEmpty(p.MainWindowTitle)).ToList();
             foreach (var process in processes)
             {
                var id = process.Id;
                var Wintitle = process.MainWindowTitle;
                Console.WriteLine("title: {0}, id: {1}", Wintitle, id);
             }

    you can try something like this to get pid and mainWindowTitle. then search the collection based on windows title.

    Good luck

    • Marked as answer by Bob Shen Tuesday, January 22, 2013 3:59 AM
    Wednesday, January 9, 2013 11:38 AM
  • You can use Process.MainWindowTitle to get process ID as follows

    Process[] AllProcesslist = Process.GetProcesses();

       foreach (Process Proc in AllProcesslist)
                {
                    if (!String.IsNullOrEmpty(Proc.MainWindowTitle))
                    {
                        Console.WriteLine("Process: {0} ID: {1} Window title: {2}", Proc.ProcessName, Proc.Id,            Proc.MainWindowTitle);
                    }
                }


    Hope is one of the best things, so I hope that your answer lies here.

    • Marked as answer by Bob Shen Tuesday, January 22, 2013 3:59 AM
    Wednesday, January 9, 2013 11:42 AM

All replies

  • var processes = Process.GetProcesses().Where(p => !string.IsNullOrEmpty(p.MainWindowTitle)).ToList();
             foreach (var process in processes)
             {
                var id = process.Id;
                var Wintitle = process.MainWindowTitle;
                Console.WriteLine("title: {0}, id: {1}", Wintitle, id);
             }

    you can try something like this to get pid and mainWindowTitle. then search the collection based on windows title.

    Good luck

    • Marked as answer by Bob Shen Tuesday, January 22, 2013 3:59 AM
    Wednesday, January 9, 2013 11:38 AM
  • Hi,

    try using GetWindowThreadProcessId()



    Gopinath.S

    Wednesday, January 9, 2013 11:39 AM
  • You can use Process.MainWindowTitle to get process ID as follows

    Process[] AllProcesslist = Process.GetProcesses();

       foreach (Process Proc in AllProcesslist)
                {
                    if (!String.IsNullOrEmpty(Proc.MainWindowTitle))
                    {
                        Console.WriteLine("Process: {0} ID: {1} Window title: {2}", Proc.ProcessName, Proc.Id,            Proc.MainWindowTitle);
                    }
                }


    Hope is one of the best things, so I hope that your answer lies here.

    • Marked as answer by Bob Shen Tuesday, January 22, 2013 3:59 AM
    Wednesday, January 9, 2013 11:42 AM
  • I am not sure who i reply to here :) 

    But, thanks for all your help everyone. I managed to come up with a similar code to the ones provided.

    private void function1()
            {
                Process[] processlist = Process.GetProcessesByName("my process");
    
                string title = "title";
                string procID;
                
                foreach (Process process in processlist)
                {
                    if (String.Equals(process.MainWindowTitle, title))
                    {
                        procID = process.Id.ToString();
                        MessageBox.Show("This is the process ID: " + process.Id);
                        MessageBox.Show(procID);
                    }
                    else
                    {
                        MessageBox.Show("Window not found");
                    }                
                }
            }

    My only problem is now, how do i use the process Id that i found in another function for example.

    a new Process.GetProcessById(procID);

    something like that.

    Thursday, January 10, 2013 12:48 AM
  • Hi TearDropZ,

    "how do i use the process Id that i found in another function for example."

    Could you explain it more clearly please? If you want to use GetProcessById method, please check related MSDN document.

    Thanks.


    Bob Shen
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, January 11, 2013 9:40 AM