Click here to Skip to main content
15,905,963 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
sometimes after use
C#
File.Copy(sourse,dest)

or
C#
file.CopyTo(dest,true)

on a big file
when I want to use or delete the destination file get the following error
C#
The process cannot access the file 'file path' because it is being used by another process.


how can i force the process of my application to release the file?
Posted
Comments
ZurdoDev 4-Jan-16 9:56am    
Wait for it to finish perhaps.

You can use a FileSystemWatcher and react to the OnChanged events. Attempt to open the file and catch any exception. Keep trying until you are successful. Note the OnCreated event is raised immediately so don't use that.

Have a look at Steve Czetty's solution on this post[^] for an example.
 
Share this answer
 
You may get this exception even when some other process accessing same file other than file copy operation.
You can check file locked or not using below method
c# - Is there a way to check if a file is in use? - Stack Overflow[^]
also you can replace copy-delete operations with single method (file move) like below
C#
if (!File.Exists(dest)) {
    File.Move(source,dest);
}
 
Share this answer
 
Please see my past answer: File used by another process exception[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900