Reputation: 9619
Here's the deal.
My WinApp is running, right? in let's say process 'A'.It creates a file and keeps the handle (keeps the file open for writing, this is a must).
Then it starts other msbuild process, let's call it 'B'. This process is started with the System.Diagnostic.Process class.
At some point, my WinApp (A) needs to delete the previously created file (remember it was created by A itself), and that's when I get an IOException with the message "The process cannot access the file 'X' because it is being used by another process". And it actually is!... If I terminate process 'B', only then 'A' can successfully delete the file.
So my questions are:
1) Is there a way I can tell the process I create not no handle the files I opened?
2) Is there another way to achieve my scenario?
Upvotes: 0
Views: 2190
Reputation: 1022
I had come across a similar problem when I was trying to read a file that was exclusively locked.
I was trying to do it using :
FileStream exclusiveWriter = new FileStream(@"C:\Temp\FileLockTest1.txt", FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
The complete discussion is available at :
File exclusively locked by another process - MSDN Forum disucssion
Hope this helps.
Upvotes: 0
Reputation: 2790
It looks like System.Diagnostic.Process.Start
calls CreateProcess with the bInheritHandles
argument set to true
.
You could try setting UseShellExecute
to true in ProcessStartInfo, or directly P/Invoke to CreateProcess
.
Upvotes: 2
Reputation: 3368
I don't know of a guaranteed way to delete a file with open handles, but if you can wait until a system restart for the file to be deleted, you can use the same technique as the MoveFile utility from Sysinternals.
This program adds registry values to the HKLM\System\CurrentControlSet\Control\Session Manager\PendingFileRenameOperations key which Windows checks on boot, and this will guarantee the file gets deleted, albeit not immediately.
Upvotes: 0
Reputation: 25834
It is possible to force a file handle closed without closing the process is using the handle, though this might cause the application to crash. In other words, doing what you wannt to do may cause 'B' to crash. That said, what you are asking for is definitely possible, since the application, Process Explorer can do this. I you search the messageboard in that link, you might find it informative, though even the act of finding which application is using a handle is an exercise in frustration, never mind actually closing the handle.
Upvotes: 0