How to list running programs in C#?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Josh Argent
    New Member
    • Dec 2010
    • 28

    How to list running programs in C#?

    I want to be able to set a string array to all the programs running (only the ones visible on the taskbar). Is this possible in C#? Thanks in Advance :D
  • bentleyw
    New Member
    • Feb 2008
    • 32

    #2
    Something like this may do it for you...

    Code:
                Process[] processRunning = Process.GetProcesses();
                StringBuilder sbProcesses = new StringBuilder();
                foreach (Process currProcess in processRunning)
                {
                    if (currProcess.MainWindowHandle.ToInt32() > 0)
                        sbProcesses.Append(currProcess.ProcessName + "\n");
                }
                MessageBox.Show(sbProcesses.ToString());
    Last edited by bentleyw; Dec 18 '10, 12:50 PM. Reason: Oops, sorry about that. I forgot the line from above. Give this a try.

    Comment

    • Josh Argent
      New Member
      • Dec 2010
      • 28

      #3
      I get an error for 'processRunning ' saying it does not exist in the current context :(. Thanks

      Comment

      • bentleyw
        New Member
        • Feb 2008
        • 32

        #4
        Not sure if it alerts you on an edit, but I've added the line of code to make it work. Good luck.

        Comment

        • Josh Argent
          New Member
          • Dec 2010
          • 28

          #5
          Thanks bentleyw, works fine now :)

          Comment

          • Josh Argent
            New Member
            • Dec 2010
            • 28

            #6
            emm bentleyw, do you know how I can set a combobox to that array? I've tried comboBox1.DataS ource = sbProcesses; but that doesn't work :( Do you know how I can do this? Thanks :D

            Comment

            • bentleyw
              New Member
              • Feb 2008
              • 32

              #7
              Sure. Assuming that the ComboBox is named "cmbProcess es", you could use a list like this:
              Code:
                          Process[] processRunning = Process.GetProcesses();
                          List<string> processList = new List<string>();
                          foreach (Process currProcess in processRunning)
                          {
                              if (currProcess.MainWindowHandle.ToInt32() > 0)
                                  processList.Add(currProcess.ProcessName);
                          }
                          cmbProcesses.DataSource = processList;
              Or you could fill a DataTable (for example, if you want to carry other info along with each process) and set the DisplayMember property of the ComboBox to the column name from the table.

              Comment

              • Josh Argent
                New Member
                • Dec 2010
                • 28

                #8
                Thanks for all your help. I can now finish my application :D

                Comment

                • bentleyw
                  New Member
                  • Feb 2008
                  • 32

                  #9
                  No problem. Glad to help. :)

                  Comment

                  Working...