Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
DirectoryInfo info = new DirectoryInfo(@"E:\Sidh\ConsoleApplication1\ConsoleApplication1\bin\Debug\f");
foreach (FileInfo file in info.GetFiles())
{
Console.WriteLine(file.Name);
file.Delete();
}

The above Code Shows An Error:
The process cannot access the file 'changes.doc' because it is being used by another process.
How to Solve This Problem?
Posted
Comments
[no name] 24-Jul-14 7:50am    
Shut down the other process that is using the file.
Wendelius 24-Jul-14 7:58am    
Now I wonder if you're trying to remove the files from application consoleapplication1. If that is the case, then it doesn't make sense if you try to remove the executable you're running...
Sid_Joshi 24-Jul-14 8:06am    
No sir I am trying to remove files from "Debug\f" Directory
Rob Philpott 24-Jul-14 8:01am    
I don't think you can. You would need the process that holds the lock to release it, or to be terminated. That could be on another computer somewhere.

Good question though, I'd be interested to know if anyone does know how you can do this.
Christian Amado 24-Jul-14 8:15am    
You cannot do it. You need to close the another process as the message says. Another way is to loop the list of active processed and kill from there but is more complicated. Just inform the situation to the user as @CPallini wrote in his Solution 1 ;)

Hi,

How our application access a file, when we try to open the file it open a ptr( Pointer) which unique for that file so if the file is no shared system will not allow to point other process to it.

How to Use Files in C : http://www.codingunit.com/c-tutorial-file-io-using-text-files[^]

& in C#

http://www.dotnetperls.com/file[^]

So if you want to access files in same time you need to share file but in terms of delete it wont solve the problem so you need to kill those process first then you will be able to delete it.
 
Share this answer
 
You shouldn't solve that 'problem'. It is not nice to delete a file while it is used by another. A workaround would be terminating the process that uses the file and then deleting the file itself (anyway not a nice behaviour).
Why don't you simply inform the user about?
 
Share this answer
 
Comments
Rob Philpott 24-Jul-14 8:00am    
It'd be news to me if you could solve that problem. AFAIK, there is no way other than determining what's got the lock and getting it to release it.
Sid_Joshi 24-Jul-14 8:02am    
Thank you for your Help but, Boss I want To delete some specious files of my Program Files Folder.
Windows not Allowed to delete this Files
That's Y i am writing this program.
CPallini 24-Jul-14 8:05am    
If Windows doesn't allow, how could your program possibly do it?
Sid_Joshi 24-Jul-14 8:10am    
By Killing the process that uses the file.
Suvabrata Roy 24-Jul-14 9:32am    
It may used by service killing that service may cause serious damage to system so please be careful while doing the same.

You can view resource monitor to view which file is using by whom (Process).
Logically its possible only when
1. Accessible folder/file is closed OR
2. Process end tasked for accessing Files/Folders by TaskManager
3. Accessible file/Folder is released from resp process.
 
Share this answer
 
Comments
Sid_Joshi 24-Jul-14 8:59am    
I agree with u mr. suresh. but how to find that process to kill. I don't know that process name. otherwise I manually killed that process from Task manager.
I Installed Kasper sky internet security and deleted files named yhwodewi.exe which located in my Program Files Directory.
 
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