Process[] wont return name of 'system' process?

  • Thread starter Thread starter Greg Merideth
  • Start date Start date
G

Greg Merideth

This piece of code creates a fault at the attempt to get the modulename
of the process when (in server2k or xp) the code gets to the "System"
prcoess.

Process[] machineProcesses = Process.GetProcesses();
foreach(Process processid in machineProcesses)
{
Console.WriteLine("pid: "+processid.Id.ToString());
Console.WriteLine("name: "+processid.MainModule.ModuleName.ToString();
}

This works if I put the code block in a try{} and include an empty
catch{} block at the end of it.

It gets the name of every process *but* the system process. Is this
because system has no "MainModule" or simply cannot get its name?
 
Greg:

Can you run this line alone without an exception?
string ModuleName;
ModuleName = Process.GetCurrentProcess().MainModule.ModuleName;

If so, try the code snippet I have here:
http://www.knowdotnet.com/articles/processes.html

You can try to use the GetProcessByName method (make sure you don't include
the extension, I think that's probably the problem.
 
That returns the name of the current running process, the code below
returns all of the processes running on the system. Its only when it
hits the 'system' process on any machine that runs a system process that
the code fails and needs the catch block.

William said:
Greg:

Can you run this line alone without an exception?
string ModuleName;
ModuleName = Process.GetCurrentProcess().MainModule.ModuleName;

If so, try the code snippet I have here:
http://www.knowdotnet.com/articles/processes.html

You can try to use the GetProcessByName method (make sure you don't include
the extension, I think that's probably the problem.


This piece of code creates a fault at the attempt to get the modulename
of the process when (in server2k or xp) the code gets to the "System"
prcoess.

Process[] machineProcesses = Process.GetProcesses();
foreach(Process processid in machineProcesses)
{
Console.WriteLine("pid: "+processid.Id.ToString());
Console.WriteLine("name: "+processid.MainModule.ModuleName.ToString();
}

This works if I put the code block in a try{} and include an empty
catch{} block at the end of it.

It gets the name of every process *but* the system process. Is this
because system has no "MainModule" or simply cannot get its name?
 
Back
Top