Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to delete a file on run time but the error showing is "cannot access a file because it is used by another process" or "cant delete a read only file".A '.dll' file is attached to the code. If '.dll' file is not attached it works fine.
Is there any way to solve the above error.

C#
if ((Directory.Exists(@"C:\\Users\\Anand\\Desktop\\temp_image)))
           {
               Directory.Delete(@"C:\\Users\\Anand\\Desktop\\temp_image, true);
           }


Here temp_image is the folder, I want to delete.
C#
Here '.dll' is camera library which is attached to another form. I want to  create the temporary folder at runtime which capture images temporary and after closing the application temporary folder(temp_folder) is deleted.Explain....


What I have tried:

Code mentioned above
Posted
Updated 25-Feb-16 2:57am
v5
Comments
dan!sh 25-Feb-16 4:03am    
What is that DLL, where it is used? Does your application create this folder?
aurj 25-Feb-16 8:41am    
here '.dll' is camera library

Because it's named "temp_image" it's likely that it contains pictures your application is using - and that's probably the problem.
If you use Image.FromFile or similar, then it acquires a lock on the file until the Image is Disposed (it's all in teh documentation: Image.FromFile Method (String) (System.Drawing)[^] - see the Remarks section)

So look at the rest of your code, and how you are handling the images and files - until you have corrected that, you can't delete the folder as your application is using files inside it.
 
Share this answer
 
Comments
dan!sh 25-Feb-16 3:56am    
The folder contains the DLL as per the question.
aurj 25-Feb-16 8:55am    
Here '.dll' is camera library which is attached to another form. I want to create the temporary folder at runtime which capture images temporary and after closing the application temporary folder(temp_folder) is deleted.Explain....
Firstly use 'Process Explorer'[^] to see which process is using the file. If the process turns out to be your project's executable file try to do the following checks

1. Make sure you properly close and (or) dispose all System.IO.StreamReader(s) and System.IO.StreamWriter(s)
2. Make sure there's no code in your class library(.dll) accessing the file at runtime

Hope you find this useful
 
Share this answer
 

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