Click here to Skip to main content
15,850,548 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am trying to delete a file after doing certain operations on it. This file is present in IIS. When trying to delete, throwing the exception. Also tried to delete manually from Explorer, it shows a message, "Being used by Web Dev Server".

How do I delete that file forcefully OR Replace that with the new version of the same file.

Thanks & Regards,
Raghu
Posted

1 solution

Do not force it, because it more like to "End task". The file being used means, that the process "Web Dev Server" is relying on this file as some of the components may be using the data provided in that file. If you force delete that file, the process would be terminated and your website would face a downtime.

To overcome this, "safely" close the process that is using it. You can use another interface or an object, that does that for you when you want to. What I mean by safely is, that your process saves the state, saves the data to the disk, ends itself in a manner that your server doesn't get halted or your website doesn't face a downtime.

A good solution to such problems is to release the resources that you are using. You may be using a Stream (FileStream specifically) to that file which is provided to your process, and you forgot to release the stream. Always remember to release the resources before terminating the processes as it leads to memory leak[^]. This case of yours, is a result of memory leak or a process using the file. If the case is "process currently using the file" then it can be closed, and resource can be released, in worse cases such as when the process is terminated but the resource wasn't released. Your file is going to remain open, and you would have to restart the machine.

In most of these cases, using[^] statement helps you release the resources automatically. I would also like to recommend using this, in your application.

Understanding the 'using' statement in C#[^]

Only then, you will be able to delete the files in a "good manner". Now of course you can just manually delete the file, let the process crash, make the website face a downtime and get your users annoyed at your 503 messages. But, that is not a good way of doing it, is it? Even replacing the file won't be done as long as the file is being used. So your process has to stop, wait until the file is refreshed, then start once again.
 
Share this answer
 
Comments
Raghuveer Kasyap 16-Nov-15 6:28am    
Thanks for the reply. I am doing introspection in an assembly and calling the methods of the other DLL. The second DLL file still shows as begin used by another Process (Web Dev Server).
Afzaal Ahmad Zeeshan 16-Nov-15 6:34am    
I would recommend opening your Task manager and looking for what is going on. Debug your application.

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