ASP.NET - The action can't be completed beacause file is open in w3wp.exe in W7
Asked By Vyom Dixit on 29-Dec-10 10:32 AM
i have a problem in deleting the files in my website directory. When i try to delete the uploaded files , i got the exception
"The action can't be completed beacause file is open in w3wp.exe" . I am using Windows 7 as OS and Visual Studio 2005 editor for creating application.
after sometime i am able to delete the file manually. But through my program , it doesn't permit me to delete the file in a follow through.
Please let me know if anyone have faced same issue and able to resolve it.
anil soni replied to Vyom Dixit on 29-Dec-10 11:03 AM
On uploading to the webserver you musting doing something with file. If you are acessing the file using any of Stream class then make sure that the Stream.Close() method is called before deleting the file otherwise the file remains in use and the resource will be release when the garbage collector will dispose the stream object.
Similary if you have use any other object then make sure that you have called Object.Dispose() (if the object supports dispose method) or any equivalent method before deleting the file.
Vyom Dixit replied to Anoop S on 29-Dec-10 11:11 AM
I am using ASP.net FileUpload control to upload a file. These are excel files and i am using OledbConnection to read the files. I am importing file data using SQL Bulkcopy operation. I have closed this connection after file imports.
then at last i am deleting the file with checking it's existence in the specified directory which resides in the application web directory's sub folder.
I have also disposed the file object. Also i used GC.Collect() for garbage collection.
But i am getting above error stilly now.
After sometime i am able to delete the file manually.
Let me know if you have any more suggestions for this issue.
Vyom Dixit replied to Vyom Dixit on 30-Dec-10 04:21 AM
I have figured out the problem which i was facing during file deletion. The bug i have found that when i was getting excel workbook sheets via oledb connection, that time i have closed the connection in finally block of try catch.
Instead of it i need to place this code just after gettting the Worksheets info from the file. Due to some improper behaviour finally block code was not working properly.
I put that code after just the gettting worksheets info in a datatable object and then i have closed the oledb connection.
Now i am able to delete excel files through my program.
Thanks to you all for help and nice suggestions.
Thanks a lot.