Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi , I upload a file like below.

C#
File_Upload.PostedFile.SaveAs(Server.MapPath("L:/.../a.jpg"));


And i would like to delete my file with the below code.

C#
System.IO.File.Delete("L:/.../a.jpg");


I faced to this error :

The process cannot access the file 'L:\chikardarin Website\WebApplication2\image_Logo\16_honda-cars-logo-emblem.jpg' because it is being used by another process.

Please help me how can i close this stream to perform my delete action ???

Edit :

my exact upload code is :

C#
string fileName = Path.GetFileName(File_Upload.PostedFile.FileName);
string imgid = "car";
string path = (ConfigurationManager.AppSettings["virtual_path"]).ToLower();
path = path + ImgId + "_" + fileName;
File_Upload.PostedFile.SaveAs(Server.MapPath(path));


now which is my resource ? is my way to upload file true ?
Posted
Updated 14-Oct-14 5:31am
v4
Comments
ZurdoDev 14-Oct-14 9:49am    
My guess is you might have other code doing something with that file. Is there a reason you are deleting it right after uploading it?
Member 11125813 14-Oct-14 11:25am    
upload action is inside one button and delete action is in another button . and i do nothing with my file which is an image . no any resize and nothing else . i just add its name into database thats all .
ZurdoDev 14-Oct-14 11:29am    
I have used fileupload many times and have never had this issue. I imagine it is IIS holding on to the file. You may want to iisreset and try delete again to make sure it actually is IIS holding onto it and not something else.
Dominic Burford 14-Oct-14 10:03am    
Between uploading the file and deleting it, what else are you doing with the file? Are you performing some other action on the file? Clearly some other process still has a lock on the file.
Member 11125813 14-Oct-14 11:25am    
i do nothing with my file which is an image . no any resize and nothing else . i just add its name into database thats all .

Please see my past answer on this problem: how to compress the error 'it is already used by another process' in vb.net[^].

—SA
 
Share this answer
 
I cannot tell you which code might be consuming the resource file. But, however, you can use this method to close the resource.

C#
resource.Close();


Mostly, streams show this kind of behaviour and you can close them once you're done working.

Recommendation

I would like to recommend you that you should use the using block while working with the resource. This ensures the correct disposal or resources, or to be technically speaking, IDisposable objects. For more, please read this document. http://msdn.microsoft.com/en-us/library/yh598w02.aspx[^]
 
Share this answer
 
Comments
Member 11125813 14-Oct-14 11:31am    
I edited my question . please reffer to the edit part . thank you
Afzaal Ahmad Zeeshan 15-Oct-14 10:43am    
You can see the Solution 2 for this. Servey has added the details in his previous answer, by using which you can easily get the process which is using the file.

Also note that, you must be viewing the previous answer, not this one.
Sergey Alexandrovich Kryukov 14-Oct-14 17:48pm    
You are right about "using", it should be the first option mentioned and explained: the end of "using" block automatically calls IDisposable.Dispose(). (I up-voted the answer with 4.)
However, we don't really know exactly what is the reason for "used by another process". This is easy to detect, if OP uses Solution 2, please see.
—SA
Afzaal Ahmad Zeeshan 15-Oct-14 10:42am    
Thanks for that. Since I didn't know that method to check which process is using it, I didn't add any vague information here. The interesting part was, somewhat the answer of mine matched to your answer and that using this also.

Also thanks for upvoting my (half) answer. And for your previous answer, I am going to give a 5. And would link it, in my answer to guide the OP in the right path.

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