C# Tips

C# Tip Article

Unable to find currently running process when debugging in VS

When running the following code in standalone application, it works fine.

string processName = Assembly.GetExecutingAssembly().GetName().Name;
var count = Process.GetProcessesByName(processName).Count();

But when running the same code within Visual Studio (by Start Debug [F5]), we get an exception since Process.GetProcessesByName() returns no process.

When running it in standalone app, the process name is the same as assembly name without .EXE extension. For example, if the EXE filename is test.exe, process name is "test" without .exe extension.

But if the code is run by VS, the actual process is not test.exe, but test.vshost.exe. So the process name is test.vshost instead of test. This is why Process.GetProcessesByName() cannot not find "test" process.