5


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

leppie
  • 115,091
  • 17
  • 196
  • 297
Amit Shah
  • 317
  • 2
  • 7
  • 15
  • but the file is in use. So why delete it? – Mitch Wheat Jul 24 '10 at 06:34
  • 2
    Duplicate - http://stackoverflow.com/questions/1040/how-do-i-delete-a-file-which-is-locked-by-another-process-in-c ? – Petar Minchev Jul 24 '10 at 06:37
  • It is not clear from your question, if you want to close a file which you opened. If this is the case, use Ed Swangren's advice. – Petar Minchev Jul 24 '10 at 06:42
  • What would the purpose be of deleting the file before closing the stream? If for argument sake you found a way to delete the file before closing the stream, what use would the stream be to you after the file is deleted? – Chris Taylor Jul 24 '10 at 06:45
  • The above code is for display the error only. I have an hosted swf file which uses the wav file. I have a back end application which modifies the same wave file which is being used by the swf. I want the updated wave to be rendered in swf. And the only way is to replace the existing file – Amit Shah Jul 24 '10 at 06:49

3 Answers3

8

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.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
2
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();
}
Mo Patel
  • 2,321
  • 4
  • 22
  • 37
  • that's not "forcefull". if the file is in use the code will throw an exception. Also File.Delete(path) as the OP has used will do the exact same thing as what you have done with FileInfo.Delete(). – Mo Patel Jun 18 '14 at 09:12
0

I am deleting the file which is used by the same process

Just close the file first...

Ed S.
  • 122,712
  • 22
  • 185
  • 265
  • and if your file is used by other processes you may try CreateRemoteThread() to close the handle. – fardjad Jul 24 '10 at 06:42
  • The above code is for display the error only. I have an hosted swf file which uses the wav file. I have a back end application which modifies the same wave file which is being used by the swf. I want the updated wave to be rendered in swf. And the only way is to replace the existing file. – Amit Shah Jul 24 '10 at 06:45
  • You assume that this is the "only way". – Ed S. Jul 24 '10 at 19:37