0

I have almost the same problem

I have two files a.exe in folder c:\one and a.exe in c:\two, both are running, but I want to delete the file only in the first folder

(If not this condition, I could use

foreach (var process in Process.GetProcessesByName("a.exe"))
                process.Kill();

and then delete the file )

For almost the same problem was the answer RunOnce registry key, but I don't care if the user logs on or not, I just want to delete it now. Can you help me?

Community
  • 1
  • 1
Vadim Volodin
  • 377
  • 1
  • 2
  • 14

1 Answers1

2

It sounds like you are trying to figure out which process corresponds to which instance of a.exe?

Seems like you can do this with the Process.MainModule.FileName property:

var exeToDelete = "...";
var exeProcess = Process.GetProcessesByName(Path.GetFileNameWithoutExtension(exeToDelete))
    .SingleOrDefault(p => StringComparer.OrdinalIgnoreCase.Equals(p.MainModule.FileName, exeToDelete));
if (exeProcess != null) { exeProcess.Kill(); }

File.Delete(exeToDelete);
ChaseMedallion
  • 20,860
  • 17
  • 88
  • 152