[.net] Force Delete a file

Started by
8 comments, last by mutex 15 years, 7 months ago
as i call the file.delete() in C# sometimes it fails to delete. how can i force the deletion to happen. some of the programs do this and i want to do this in C#. Thank You.
Advertisement
What do you mean by "fails to delete"? If you consult the File.Delete documentation[1] you'll see that File.Delete will fail if the given parameter is invalid in some way *or* the file is locked by another application.

And if that's the case, there's no easy way to delete it. :)

[1] http://msdn.microsoft.com/en-us/library/system.io.file.delete.aspx
Quote:Original post by eGamer
how can i force the deletion to happen.


You can't. Some files are protected against deletion (because the program you are running does not have the required permissions) and others are locked by other programs and as such cannot be removed until the other lock disappears.

The only solid way of achieving this is to 1° run the program as an administrator (or some other user with write access to the file) to evade lack of permissions and 2° to stop all other processes (possibly by rebooting and running before all other programs) to evade locks on the files. And even then, some files may be locked from the very beginning, for instance certain system files.

You could try MoveFileEx function (in Kernel32.dll) with the MOVEFILE_DELAY_UNTIL_REBOOT flag.
(In c# you can use such functions with DllImport attribute)
Quote:MSDN:
If dwFlags specifies MOVEFILE_DELAY_UNTIL_REBOOT and lpNewFileName is NULL, MoveFileEx registers the lpExistingFileName file to be deleted when the system restarts.
well how can you explain the unLocker programs which has the ability to delete the file even if it was used by other processes, they are plenty on the internet.

may be i wany an api function that:
1. has previleges of an administrator
2. has the ability to delete the file [force delete].


??
Quote:Original post by eGamer
well how can you explain the unLocker programs which has the ability to delete the file even if it was used by other processes, they are plenty on the internet.

may be i wany an api function that:
1. has previleges of an administrator
2. has the ability to delete the file [force delete].


??


The unlockers you refer to require debug process privileges, which are not commonly available in locked down environments. They require these permissions in order to list the open file handles.

There are no API calls which allow you to escalate your privileges to administrator without providing credentials. These sort of API calls are the black hat's attack vector of choice. If you think you've found one I'll bet money that somewhere credentials are stored which are used to escalate the account's privilege.

The applications you reference work by forcibly closing the file handles other applications have open on a particular file. This is unsafe and in my opinion your applications should never be making this choice. (If it's a file YOU created that you want to delete then search your code and find where you fail to close it.)

Finally, network resources cannot always be released. Or, the medium you're trying to delete from may be read only for a variety of reasons. (Read only media, failure to have appropriate permissions, etc.) [Unlocker fails on these types of resources.]

Read: You can NOT guarantee a file will be deleted. Ever. And trying to force the issue is a dangerous path to tread.
..what we do will echo throughout eternity..
so How can i get the list of processes that are using a specific file, and then be able close these processes and delete that file?
1. Iterate through the available processes.
2. For each process, examine the modules filename which are currently held by the process; determine if it applies to you or not.

Process[] processList = Process.GetProcesses(".");foreach(Process process in processList){     if(!process.HasExited)     {          ProcessModuleCollection modules = process.Modules;          foreach(ProcessModule module in modules)          {               // Examine module.FileName          }     }}

This method lists only the executables and dynamic libraries that have been loaded by a process.

To determine which process is holding a lock on any file type (or an executable or DLL that wasn't loaded as a resource) you'll have to resort to undocumented Win32 API calls using PInvoke. (NtQuerySystemInformation and NTQueryObject, for instance; check out http://www.pinvoke.net/ for help with those.)

You may be able to get information about the locked files using WMI, but I haven't munged with WMI and can't help you there.
..what we do will echo throughout eternity..
System.GC.Collect();
System.GC.WaitForPendingFinalizers();
File.Delete(fileName);


Try this..It worked for me..Hope for you also...(My program gave me headache for 4 days until I tried this)..
Quote:Original post by eGamer
so How can i get the list of processes that are using a specific file, and then be able close these processes and delete that file?
Be careful. You risk crashing those processes if they don't expect the files to be closed from underneath them.

This topic is closed to new replies.

Advertisement