Jump to content

Unable to delete file


cothedo
 Share

Recommended Posts

I have helped build a basic content management system where you can upload and delete files. There is a special page where you can view and delete a file by clicking a button. The event of the button is:System.IO.File.Delete(Server.MapPath(...filename...))However, I get an error message saying "The porcess cannot acces the file (...filename & path...) because it is being used by another process."I just don't understand why I get this error to delete a jpg file. I have the rights to the directory the file is deleted from.Any help is appreciated.

Link to comment
Share on other sites

  • 1 month later...

Well, I found the answer, and for anyone interested, I'll just post it here. My code was right, but what was needed was something called "Garbage Control". After the file has been used or called for in the code behind, use the following code:GC.Collect()That will release the hold on the file and thus make it available again.

I have helped build a basic content management system where you can upload and delete files. There is a special page where you can view and delete a file by clicking a button. The event of the button is:System.IO.File.Delete(Server.MapPath(...filename...))However, I get an error message saying "The porcess cannot acces the file (...filename & path...) because it is being used by another process."I just don't understand why I get this error to delete a jpg file. I have the rights to the directory the file is deleted from.Any help is appreciated.
Link to comment
Share on other sites

It ould be better to store the file object like and then dispose of the object instead of calling the GC which is harder on performance

FileStream fs = System.IO.File.Delete(Server.MapPath(...filename...));fs.Close();fs.Dispose();

Link to comment
Share on other sites

It ould be better to store the file object like and then dispose of the object instead of calling the GC which is harder on performance
FileStream fs = System.IO.File.Delete(Server.MapPath(...filename...));fs.Close();fs.Dispose();

Thank you, I'll see if I can get this to work
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...