8

I need to know whether a process is already running but I don't want to hardcode its name. I'd rather derive it from the *.exe so I performed some experiments and created a simple console application where I set the assembly name in project properties to foo and then renamed the foo.exe to bar.exe. I checked the running processes and indeed it was bar that was running.

Is it by design and I can rely on this behavior or can a process have a different name from the exe and does it apply to all kinds of exe files or only to .NET assemblies?

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
t3chb0t
  • 16,340
  • 13
  • 78
  • 118

1 Answers1

6

You can't change the image name of the process. That is set by Windows and not changeable. It will use the name of the executable to set it, so it is safe to use.

Instead of using the process to get the executable name, you could simply use the assembly name from .NET:

System.AppDomain.CurrentDomain.FriendlyName
Community
  • 1
  • 1
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • And yet, `explorer.exe` appears as "Windows Explorer" in Task Manager – Panagiotis Kanavos Feb 03 '16 at 10:19
  • Did you open the Details tab? That is the real process name and not the window title or file description. It shows only `explorer.exe` to me. – Patrick Hofman Feb 03 '16 at 10:20
  • Oh, I didn't know it's called _image name_ :-) Actually I'm doing a slimilar thing to the OP of the question you've linked, I'm launching workers and wanted to know whether they are already running. – t3chb0t Feb 03 '16 at 10:20
  • @t3chb0t there are far better options for this than checking a string descriptor, eg checking the status of whatever IPC mechanism you use. – Panagiotis Kanavos Feb 03 '16 at 10:21
  • @PanagiotisKanavos by better you mean easier or more reliable? I wanted the root process to check whether it can start a particular worker and this one only knows thier exe paths so now knowing that the exe name equals process name I can for sure tell whether it's already runnig or not without having to hardcode any names. They won't be communicating any time soon I think. – t3chb0t Feb 03 '16 at 10:27