Reputation: 137
I Use mvc and i have a controller that takes a file name and deletes it from server, when I use visual studio development server on my local machine, This Page(controller) successfully delete the file but on production server I'm getting the following Error:
The process cannot access the file 'filename' because it is being used by another process
System.IO.File.Delete(Server.MapPath("~/content/myFolder/" + id));
before this line of code I just do a custom request validation That dont access the file. This File has been saved by another Page Right before this page.
In fact I want to know Which Process May be accessing this file. and if any code from previous pages(controllers) may cause this Problem? i have searched but I found nothing useful.
Upvotes: 1
Views: 3137
Reputation: 1958
When using object of type which implement IDisposable interface, you should call dispose
method as soon as you no longer needed object.
CLR's Garbage collector is smart and it release resources when it perform garbage collection. but the problem is garbage collector doesn't collect object as soon as control leaves the current scope. but it will collect it when memory of generation 0 is full or you force by calling collect
method in your code.
so better to clean up resources when they are no longer needed.
Upvotes: 0
Reputation: 1326
You could check the identity under which the application pool of your application runs, then check the permissions for that account. To check which process locked the file, try this link.
Upvotes: 1