System Processes

To work with system processes, either local or remote ones, you should use the System.Diagnostics namespace’s Process class. I wouldn’t waste time on describing processes, let’s see what are the objectives here:

  • Get a list of running processes
  • Retrieve information about the current process
  • Get a list of modules loaded by a process
  • Start a process by using and not using command-line arguments

 

Get a List of Running Processes

To get a list of running processes, use the Process.GetProcesses static method. Optionally, you can include a machine name as parameter for remote connections. To filter processes by name you should use the Process.GetProcessesByName with the friendly name of the process you seek. Both of these methods return a strongly-typed Process array. An example code:

foreach(Process p in Process.GetProcesses())
{
  Console.WriteLine(p.ProcessName);
}

To kill a process, call the Kill method of the process instance.

Retrieve Information about the Current Process

The Process class has a static method called GetCurrentProcess, which gets the current process for you. There are a whole lot of properties of the Process class. Examine the MSDN for further understanding.

Get a List of Modules Loaded by a Process

Processes host modules. To access these modules, you should iterate over the Modules property of a given process instance. This property provides a strongly-typed collection of ProcessModules (a ProcessModuleCollection). A syntax:

foreach(ProcessModule pm in Process.GetCurrentProcesses().Modules)
{
    Console.WriteLine(pm.ModuleName);
}

Start a Process by Using and by not Using Command-Line Arguments

To start a process, call the static Process.Start method. It provides the following overloads:

ProcessStartInfo pinfo
String FileName
String FileName, string Arguments
String FileName, string UserName, SecureString Password, string domain
String FileName, string Arguments, string UserName, SecureString Password, string Domain

 

Now I think you can start a process by using or not using command line arguments. A ProcessStartInfo is a helper class with three constructors, the default, one that accepts a single filename string, and one that accepts filename and arguments as string.

StackTrace and StackFrame

A StackFrame is an object that’s being created at every method invocation. It always contains MethodBase information, optionally file name, line number, etc. The best way to view a StackFrame in action is to raise an exception, and view its StackTrace property.

A StackTrace is an ordered collection of StackFrames. You’ll use both of these classes in debugging scenarios.

Further Reading

Process Class
StackFrame Class

Advertisements

Tags: , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: