Vad
Vad

Reputation: 898

Process locks a folder

Have pretty odd situation. There are 2 applications:

1) C:\MyFolder1\First.exe

2) C:\MyFolder2\Second.exe

First.exe runs Second.exe and quits.

Process.Start(@"C:\MyFolder2\Second.exe");

// And exit.

Seconds.exe waits a few seconds and tries to remove "C:\MyFolder1\" folder.

// Wait for 5 seconds - First.exe terminated by that time for 100%

Directory.Delete(@"C:\MyFolder1\", true);

Action fails with “The process cannot access the file ‘C:\MyFolder1\’ because it is being used by another process.” It's able to remove the First.exe file (actually all files in the folder), but not the folder itself.

Does anybody have an idea why the folder is locked by the second process?

Upvotes: 2

Views: 1072

Answers (3)

Mario The Spoon
Mario The Spoon

Reputation: 4869

Use Process.WaitForExit to make sure it is done (and possibly avoid those 5 seconds)

Upvotes: 0

Hans Passant
Hans Passant

Reputation: 942538

A process has a default working directory. You set its initial value with the ProcessStartInfo.WorkingDirectory. You cannot jerk that floor mat, it keeps a lock on that directory until the process terminates or it changes its working directory with Environment.CurrentDirectory. The default working directory for Second.exe is C:\MyFolder1 since you didn't set it.

Upvotes: 4

Hoai-Thu Vuong
Hoai-Thu Vuong

Reputation: 1967

I don't think First.exe is stopped. Please check process tree. (Folder 1 is locked by this process)

Upvotes: 0

Related Questions