Click here to Skip to main content
15,903,724 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hello everyone i have done with processing of files from source to destination folder but my intention is to delete the files in source folder after processing is complete.

but im getting the error like "The process cannot access the file 'C:\Datacap\APT\Images\Input\APT001.tif' because it is being used by another process."

What I have tried:

hello everyone i have done with processing of files from source to destination folder but my intention is to delete the files in source folder after processing is complete.

but im getting the error like "The process cannot access the file 'C:\Datacap\APT\Images\Input\APT001.tif' because it is being used by another process."
Posted
Updated 19-Jun-16 23:52pm

You have to identify the process that has the file opened.

To identify the processes you can use the Process Explorer[^] (press Ctrl+F or open the Find menu, select file handle or DLL, and enter the file name).

If it is your application, just ensure that the files have been closed before deleting them.
 
Share this answer
 
Comments
NagaNimesh 11474558 20-Jun-16 4:54am    
Thanq for Replying.but i cant understand what i need to do now u said process explorer is i need to invoke the .dll in to my application???
Jochen Arndt 20-Jun-16 5:30am    
Use the Process Explorer to know which process has the file opened:
- Start it
- Press Ctrl+F or select "Find - Find Handle or DLL" (menu item)
- Enter the file name in the "Handle or DLL substring" input field
- Press "Search"
- You will get a list of processes that has the file opened

If it is another process, you / the user must manually close the file in that process. It can't be closed by your application (while it is possible to do so, it is a don't do so).

If it is your process, check your code where the file might have been opened. Ensure that the file is closed in your code when not needed anymore.

It is your responsibility as a programmer to close files after opening when all operations on it has been finsihed.

If you have for example opened a file using the File.Create() method and the file/stream object is not in a local scope, use the File.Close() method when the work is done. Otherwise, the file will be closed when the file/stream object goes out of scope. If you try to delete the file before this happens, you will get the error "being used by another process".
NagaNimesh 11474558 20-Jun-16 4:56am    
please tell me how to close the files??
NagaNimesh 11474558 21-Jun-16 0:31am    
tanq Jochen arndt for your suggestion.I have done my task bye using following solution .
System.GC.Collect();// Forces an immediate garbage collection of all generations.

System.GC.WaitForPendingFinalizers();// will stops all the current running threads

now you can delete your files and folders..like below

System.IO.DirectoryInfo di = new DirectoryInfo("your path");

foreach (FileInfo file in di.GetFiles())
{
file.Delete();
}
foreach (DirectoryInfo dir in di.GetDirectories())
{
dir.Delete(true);
}
 
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