I am developing a application which requires to delete a file no matter it is being used by other process
Consider the following snippet of code.
using System;
using System.IO;
namespace DotNet_Concepts.File_Operation
{
class Deleting_File_Which_Is_In_Use
{
static void Main(string[] args)
{
StreamReader lclFileStream = null;
string lclFileName=string.Empty;
try
{
lclFileName=@"E:\Visual Studio 2008 Projects\DotNet Concepts\DotNet Concepts\Local Files\Garbage.txt";
if (File.Exists(lclFileName))
{
lclFileStream = new StreamReader(lclFileName);
if (lclFileStream != null)
{
//Doing some operation
}
//Deleting the file before closing the stream
File.Delete(lclFileName);
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.StackTrace);
}
Console.ReadLine();
}
}
}
I am deleting the file which is used by the same process. Is it possible to delete the file
Thanks, Amit Shah
If the file you want to delete is in an “exe” file of a program, try closing the program first, then attempt to delete the file again. You can also try restarting your PC to close down any running programs or closing the apps that might be using the program you want to delete.
To check the full path of a file or folder, you can right-click the file or folder and choose Properties. Use “RMDIR /S /Q” command to force delete a folder in CMD: After entering Command Prompt window, you can type the rmdir /s /q folder path, for example, rmdir /s /q E:\test, and press Enter key.
In the command window, type the DEL /F file name command and press Enter to force delete the file that is in use.
Use Shift + Delete to Force Delete File/Folder. You can select the target file or folder and press Shift + Delete keyboard shortcut to delete the file/folder permanently. This file deletion method won't pass the Recycle Bin.
Yes, this is a known Windows security concern, known as a "handle recycle attack". It is technically possible to walk the undocumented kernel handle table, inject code in the process that owns the handle and call CloseHandle() to close the file handle. Very hard to exploit, you need admin rights to do this. Much better ways to screw up a process if you have that right.
What results is more of a Denial Of Service attack, one that randomly fecks up the machine with no way to find out how it happened. The process is not aware that the file handle was closed, it didn't close it, and just keeps writing to the handle. The written data falls into the bit bucket. Ignoring the return value of WriteFile() is pretty standard in most unmanaged programs.
Until the process opens another handle to write to, say, a database. Handles are recycled, it can get the same handle value back. Now the process is writing the normal database data, intermixed with the data that was supposed to go in the now-closed file.
Hilarity ensues when anybody tries to read the data back from that database. Or whatever other resource got destroyed, it's random. You are kinda covered though, nobody will be able to figure out that it was your program that destroyed the machine.
using System.Collections;
using System.Diagnostics;
using System.Management;
if (File.Exists(@"D:\New folder\Test0001.wav"))
{
GC.Collect();
GC.WaitForPendingFinalizers();
FileInfo f = new FileInfo(@"D:\New folder\Test0001.wav");
f.Delete();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With