How to close a file that is already opened in a directory

15,072

Solution 1

Please let me know where I am missing.

Well you're closing your handle on the file - but that's not going to affect any other file handles, whether they're owned by your process or not. Basically you can't delete a file which is in use elsewhere, unless it's been explicitly opened with a file share flag which allows it to be deleted. That's just the way the Windows file systems work, as far as I know. (EDIT: I originally suggested using Notepad as a way of keeping a file handle open, but apparently it closes the handle after loading it into memory. Um, try other apps :)

Solution 2

You cannot delete a file if handle is open by another process. You will have to kill that process first before you can delete the file.

You can look at this example to see how to find all open handles.

Share:
15,072
Sneha S Murthy
Author by

Sneha S Murthy

Updated on August 21, 2022

Comments

  • Sneha S Murthy
    Sneha S Murthy over 1 year

    I am trying to parse a folder and delete all the files in it.

    DirectoryInfo dir = new DirectoryInfo("C\\Temp");
    if (dir.GetDirectories().Any(p => p.Name == "\\NewTemp"))
    {
        foreach (string file in Directory.GetFiles(dir + "\\NewTemp"))
        {
            File.SetAttributes(file, FileAttributes.Normal);
            File.Delete(file);
        }
    }
    

    This code works fine and deletes all the files in my \NewTemp Folder. But if any of the files is opened those files will not be deleted. I want to forecfully close the files that are opened and delete them. I even tried

    foreach (string file in Directory.GetFiles(dir + "\\NewTemp"))
    {
        TextReader tr = new StreamReader(dir+"\\NewTemp\\"+file);
        tr.Close();
        File.SetAttributes(file, FileAttributes.Normal);
        File.Delete(file);
    }
    

    But no use. Please let me know where I am missing.

    • Sam I am says Reinstate Monica
      Sam I am says Reinstate Monica over 11 years
      I'm pretty sure there's this command line utility that you can use to get all the processes that have a particular file open, and I'm also pretty sure you can taskkill those processes from c#
    • Sneha S Murthy
      Sneha S Murthy over 11 years
      tr is trying to close the file TextReader tr = new StreamReader(file). I have made a typo
    • 2GDev
      2GDev over 11 years
      stackoverflow.com/questions/177146/… It's not too simple with managed code. In this answer there is a link to a proget on Codeproject that can help you.
  • Shahar Gvirtz
    Shahar Gvirtz over 11 years
    Well, actually you can delete file that you opened in notepad from the explorer. It's all depends on the FileShare that you open the file with... so, sometimes you can delete files that are in use elsewhere. it's all depends which FileShare level the app that opened the file needed.
  • Kratz
    Kratz over 11 years
    I believe you can force close file handles (any file handle), I've used programs that do it. I don't think there are any .Net functions to do it, so you might have to dig into the windows API for an answer.
  • Devin Burke
    Devin Burke over 11 years
    That's actually a bad example. NotePad reads the file and then closes it. You can delete files opened with NotePad without closing NotePad first.
  • loopedcode
    loopedcode over 11 years
    Notepad example was incorrect. Notepad doesn't keep handle open. It loads all text in memory and releases the handle. You can delete the file even when Notepad is open.
  • Daniel Hilgarth
    Daniel Hilgarth over 11 years
    @Kratz: That's a highly non-trivial task. You need to somehow get the processes that hold a handle to the file and make those processes close that handle. There is no single WinAPI function for that.