How to delete a file that is using by windows?
Asked Answered
E

4

1

Is there any way to delete a file when its using by any program or other process in windows? I searched and found this 2 ways:

1- using RunOnce key in Registry; I'm not gonna use this because i dont want to wait for windows restart or anything else... prefer to do it ontime!

2- using the way declared in this page: http://www.delphipages.com/forum/showthread.php?t=201190 the problem here is its useful under NT windows, i need a way works on all Windowses!

Thank you.

Excrete answered 24/3, 2011 at 21:39 Comment(3)
Your option 2 waits for a reboot. Are you really wanting to support 98/ME? You can't delete a file while something else has it open. You;d be disappointed if you opened a file and then somebody else deleted it before your'd finished.Mellie
What file could you possibly want to delete while it is currently being used? And can't wait until reboot?Meiny
Note: The MoveFileEx(... MOVEFILE_DELAY_UNTIL_REBOOT) method will only work if the user has admin privileges, otherwise if will return ACCESS_DENIEDBrietta
I
4

the problem here is its useful under NT windows, i need a way works on all Windowses!

All modern desktop Windowses (XP, Vista, 7) are also NT. Do you really need to work with NT<4 or Win98? Or even Win CE/Mobile/Phone? Probably not.

If you need to delete an open file straight away, about the only thing you can do is attach to each process using debugger privileges, see if it has any handles open on the file, and if so close them underneath it. You can do this the manual way using eg Process Explorer. Many applications won't react well to having their files closed on them; expect them to exception out when they try to do something with the dead handle.

Unfortunately there is no option in Windows to have Unix-style files that can exist attached to a file handle independently of being stored under a filename on disc.

Ileneileo answered 24/3, 2011 at 22:7 Comment(3)
well .. this will cause a lot of problems with the app you attached, i dont think this is the right thing to do.Anorexia
Indeed, it rarely is. It can be useful for dealing with an application that has handle leaks (including Explorer!) or for killing malware, but it's not an everyday tool.Ileneileo
"expect them to exception out when they try to do something with the dead handle" If I'm recalling correctly, it's even worse than that. The handle may be recycled and the program might end up operating on some entirely different file. You're lucky if you only get an exception!Brainwashing
U
1

Try MoveFileEx with MOVEFILE_DELAY_UNTIL_REBOOT flag. Will postpone move or delete action until reboot.

Edit: If you don't whant to restart the only option is to close those handles. ProcessExplorer does that and works all the time and I have not seen any process to crash. See more info about enumeration handles in a process at http://www.codeguru.com/forum/archive/index.php/t-176997.html. But keep in mint that you should enumerate all processes in the system and behave different on Vista+ (you need to be elevated)

Unwished answered 24/3, 2011 at 21:47 Comment(2)
But this is helpful, since what he wants can't really be done.Kowloon
See here: #14530752Commandment
A
1

You cant delete a file when someone is using it. No matter how hard you try, windows will not let you. It can work with some files, but in general it does not work. What you can try is postpone the deletion, when no one is using the file. You can:

1 - use RunOnce, but you dont want that.
2 - Wait in a loop, trying to delete the file. Pseudo code:

DeleteFile  
Check if you was able to delete or if file still exists.  
if you are able to delete, then exit loop.  

That is the best you can do, and what i could remeber.

Anorexia answered 24/3, 2011 at 21:48 Comment(5)
Why people give -1 without any explanation? I would like to know what is wrong with my answer.Anorexia
why +1 ? with this loop you may wait until reboot. OP needs the action, not the naive loop. so it's not the best he can do. so I vote for the solution aboveSepaloid
Its not a naive loop. He can add a timout to he function, so he wont get stuck forever in the loop. soon or later, the app wich has the file open will close the file and the loop will be able to delete the file. Also, its not a good idea to close other apps handle.Anorexia
Anytime you suggest polling you are offering up down-vote bait.Mellie
I agree that's not fair and dangerous to kill foreign processes when they're using some file, but that was the question ;)Sepaloid
F
-1

Your files are most likely locked because some process has a handle open to them. That is the most common reason for the Access denied result when deleting or moving a file.

A really blunt way is to close that handle.

Then Handles tool from SysInternals (you can download the sources too) can do that for you: I have been successfully using that.

Note 1: You need administrative privileges to use it.

Note 2: Closing a handle from another process is considered very rude, and can make that process unstable.

Footsore answered 25/3, 2011 at 14:34 Comment(4)
I think the OP already had this much established, you're just re-stating the problem. What's the solution?Commandment
A solution is to forcefully close the handles for those files.Footsore
Using a third-party application? The question was about writing code to do this, not doing it manually. Of course you need to close the handle, that's the common sense part. Anyway I was just trying to explain why I think you were down-voted.Commandment
Ah, ok, now I get what you mean. Thanks! I just thought the handle part wasn't common sense, hence the answer including the mentioning there is source code available.Footsore

© 2022 - 2024 — McMap. All rights reserved.