Hossein Hagh
Hossein Hagh

Reputation: 127

how to release file that is in use by another program

How to release file that is in use by another program in c#?

if(!IsFileLocked(fileName))
{
    // write in file
}
else
{    
   // first   ReleaseFile(fileName);    
   // two     write in file    
}

Upvotes: 0

Views: 2792

Answers (2)

lordkain
lordkain

Reputation: 3109

duplicate question How to release a handle through C#?

use PInvoke if you have an handler that you want to close

[System.Runtime.InteropServices.DllImport("Kernel32")]
private extern static Boolean CloseHandle(IntPtr handle);

Upvotes: 0

Tobberoth
Tobberoth

Reputation: 9527

You can't control other processes locking of files in C# natively. Your only option is to use Process.Kill to kill the processes locking the file, assuming you know which processes those are.

Upvotes: 2

Related Questions