Sunday, May 20, 2007

Getting processes on local or remote machine

The Process class in System.Diagnostics namespace, provide information about processes on current or a remote machine.

You can get list of all process on your local machine by this line of code:

Process[] process = Process.GetProcesses();

or if you want to have a list of a remote computer process list:

Process[] processList = Process.GetProcesses("machineName");

You can also use IP instead of computer name if desired.
There are also some static methods that help you to get specific process by it 's Name/Id on local or a remote computer.

Process proc = Process.GetProcessesByName("notepad");

Then you can get some information about the process. For instance in the line below I 'm getting the process filename from the MainModule property:

foreach(Process proc in Process.GetProcesses())
{

Console.WriteLine(" ProcessName : {0}, File Name: {1}",proc.MainModule.ModuleName, proc.MainModule.FileName);
}


If you want to stop a process you can use the Kill method on that process. But notice that if the process cannot be terminated you will get a Win32Exception or if the process has already exited you will get an InvalidOperationException.

It 's important to know when you are using Kill method, that you can only Kill local processes and if you try to terminate a remote process by calling Kill method, you will get a SystemException.

11 comments:

Payal Bansal said...

nice explaination....but can u please explain more about the exceptions generated

Unknown said...

ok that's good but why I get an exception when trying to enumerate processes on a remote machine

Masoud Tabatabaei said...

What kind of exception you are getting?

Anonymous said...

If you want to kill a remote process you can use the taskkill command from the console.

This can then be added to C# code:

ProcessStartInfo processStartInfo = new ProcessStartInfo("taskkill", "/s " + txtComputerName.Text + " /im notepad.exe");

Process proc = new Process();

processStartInfo.RedirectStandardInput = true;
processStartInfo.RedirectStandardOutput = true;
processStartInfo.RedirectStandardError = true;
processStartInfo.CreateNoWindow = true;
processStartInfo.UseShellExecute = false;

proc.StartInfo = processStartInfo;
proc.Start();
proc.WaitForExit();

StreamReader streamReader = proc.StandardOutput;
StreamReader err = proc.StandardError;

string tempLine = string.Empty;

while (!streamReader.EndOfStream)
{
tempLine = streamReader.ReadLine().Trim().ToLower();
if (tempLine.Contains("success"))
{
MessageBox.Show(tempLine);
}
}

The command does have additional switches that allows you to add username and passwords.

alrarea said...

hi
nice to see your work

is it work for all case. when i did me got one exception saying
Could not connect with Remote Computer

How i enable the connection. I can do every thing (file transfer, net send, desktop sharing using netmeeting all with that computer)

Can you help me on that ?

Thanks

Mikey said...

I am guessing that to get a process list for a remote machine then you will need some kind of administrative access to that machine?

Anonymous said...

yes thats exactly what you need
administrative access to the remote machine over the network

RRave said...

Dear Sir,

I have a launched new web site for .NET programming resources. www.codegain.com. I would like to invite to the codegain.com as author and supporter. I hope you will joins with us soon.

Thank You
RRaveen
Founder www.codegain.com

Namrata Kannan said...

I am trying to retrieve the applications running on the remote machine.I used the following code snippet:
StringBuilder sb = new StringBuilder();

foreach (Process p in Process.GetProcesses(machinename))
{
try
{
if (p.MainWindowTitle.Length > 0)
{
sb.Append("Window Title:\t" + p.MainWindowTitle.ToString() + Environment.NewLine);
sb.Append("Process Name:\t" + p.ProcessName.ToString() + Environment.NewLine);
sb.Append("Window Handle:\t" + p.MainWindowHandle.ToString() + Environment.NewLine);
sb.Append("Memory Allocation:\t" + p.PrivateMemorySize64.ToString() + Environment.NewLine);
sb.Append(Environment.NewLine);
}
}
catch { }
}

here the machine name i get from the user using a textbox.
However,i am gettin an InvalidOperationException:Could not access remote machine.
Am using a Vista machine,Visual Studio 2008,.Net Framework 3.5

Anonymous said...

blah, p.MainWindowTitle can execute only on local machine..

Anonymous said...

This is all shit programming... If you have the username and password of the remote machine, why don't you use WMI classes. These are perfect in these kind of scenarios. - Bravo