List all Running Processes

From IronPython Cookbook

From CountZero.

This example shows a system message box with all the currently running processes.

It uses the StringBuilder class to build up a string containing a list of all the currently running processes. It gets this list from the Processes.GetProcesses Method.

When run, it shows something like:

A message box containing all currently running processes.

import clr
clr.AddReference("System.Windows.Forms")
from System.Windows.Forms import MessageBox
from System.Diagnostics import Process
from System.Text import StringBuilder

sb = StringBuilder()
procs = Process.GetProcesses()
for p in procs:
  sb.Append(p.ProcessName)
  sb.Append("\t")
MessageBox.Show(sb.ToString(), "Running processes")

A more Pythonic (and shorter) way to do this would be to use the built string join method and a generator expression:

import clr
clr.AddReference("System.Windows.Forms")
from System.Windows.Forms import MessageBox
from System.Diagnostics import Process

procs = Process.GetProcesses()
longString = '\t'.join(proc.ProcessName for proc in procs) 

MessageBox.Show(longString, "Running processes")

On Mono, only processes started by Mono runtime can be listed. You will see processes you launched as in Launching Sub-Processes article, but you won't see other processes running on the same system.

Back to Contents.

TOOLBOX
LANGUAGES