Finding a process from an executable (.NET)

Is there an easy way to pass a fully qualified path to an executable to System.Diagnostics.Process (or any other .NET process related class/object) and get back a Process object? GetProcessesByName doesn’t work with a path name or an executable, as follows:


//this returns a zero length array
a_process= System.Diagnostics.Process.GetProcessesByName(@"C:\Program Files (x86)\Mozilla Firefox\firefox.exe")

//this works, a_process[0] will be our process
a_process= System.Diagnostics.Process.GetProcessesByName("firefox")

Right now i’m doing it the long way (pseudo) :


target_executable= @"C:\Program Files (x86)\Mozilla Firefox\firefox.exe"
get all active processes
for each process in all active processes
  if process is not null
      get filename from process module
      return process if filename matches target_executable

is there a better way to do this?

Process has a property called MainModule that has the information your looking for.

So this should give you what you need:

filePath = a_process[0].MainModule.Filename

[QUOTE=senkusha;3439]Process has a property called MainModule that has the information your looking for.

So this should give you what you need:

filePath = a_process[0].MainModule.Filename

[/QUOTE]

this much i know, i’m trying to avoid having to iterate through every active process and poll MainModule for each one.

Can’t you take the substring between the last forward slash and the dot before the extension?

I dont know those string methods from the top of my head but i guess you know what i mean.

“C:\Program Files (x86)\Mozilla Firefox\firefox.exe”
getridofthispart\findthis.getridofthispart

Pass through what remains.


LovelyWendie99

And I would have known that you knew that, had I read the pseudo code all the way through. Shame on me!

Looking through the Win32 API docs, there doesn’t seem to be an exposed function that would take the image path and return the process handle. GetProcessByName seems to be a method provided solely by the .NET Framework, and is not listed as a process function in the API docs. .NET Reflector shows that GetProcessByName has to iterate over the list of running processes.

I’d say what your doing is fine, unless something about it just really bothers you.

[QUOTE=senkusha;3443]
I’d say what your doing is fine, unless something about it just really bothers you.[/QUOTE]

Hehe, yeah that’s really all it is, just some mental snobbery.:D: It definitely works well enough the way it is, just wanted to make sure there wasn’t something i was missing.

[QUOTE=Gungnir;3442]Can’t you take the substring between the last forward slash and the dot before the extension?

I dont know those string methods from the top of my head but i guess you know what i mean.

“C:\Program Files (x86)\Mozilla Firefox\firefox.exe”
getridofthispart\findthis.getridofthispart

Pass through what remains.[/QUOTE]

Yeah i tested that and it works, i was just hoping for something a bit cleaner…My years of MEL coding have instilled in me a deep hatred of string munging.:laugh:

:laugh: I read your post like five times to make sure i understood what you wanted because i thought it was weird you had not considered doing it like that. But now i understand, even though i have no MEL experience manipulating strings can be a hassle in many languages :x:

Not sure i helped in any way but it was my pleasure.


FERRARI 248 F1 SPECIFICATIONS

[QUOTE=Gungnir;3459]
Not sure i helped in any way but it was my pleasure.[/QUOTE]

Hehe no i was definitely glad for the input. Sometimes it’s nice to get confirmation of a workflow just to be sure that you didn’t skip over something!