简体   繁体   中英

File.Delete fails silently - how to debug?

The following code fails to delete the file and prints file delete: so I know the file exists. With my app open I am able to delete the file in file explorer.

How can I debug this?

1) File permissions? My app created the file so should be able to delete? Regardless it would throw an exception and hit my catch debug message.

2) The file exists! According to the docs any other failure besides non-existence should be caught in my catch...

if (File.Exists(fn))
{
     Debug.WriteLine("file delete: " + fn);
     try
     {
         File.Delete(fn);
     }
     catch
     {
         Debug.WriteLine("Could not delete: " + fn);
     }
} else {
     Debug.WriteLine("File doesn't exist: "+fn);
}

The file is saved from a RichTextBox using the following code if this matters.

TextRange range;
FileStream fStream;
range = new TextRange(mNotepad.Document.ContentStart, mNotepad.Document.ContentEnd);
fStream = new FileStream(fn, FileMode.Create);
range.Save(fStream, DataFormats.XamlPackage);
fStream.Close();

Deleting a file does not provide a guarantee that the file will actually be removed from the file system. The file might have been opened by another process, which explicitly specified delete sharing . Very similar to read and write sharing. Also available in .NET, you'd pass FileShare.Delete to the FileStream constructor.

But the physical file can of course not be removed until all processes close the file. So it lingers beyond the File.Delete() call, can be seen by File.Exists() as well. Opening the file can no longer work, that will be rejected with access denied. Otherwise an excellent reason to never use File.Exists(), it has many problems.

If you want to find out what other process has the file opened then you can use a utility like SysInternals' Handle or Process Explorer. Expect to find back a program like a virus scanner or search indexer, could be anything however. Like a .NET program :)

From MSDN:

If the file to be deleted does not exist, no exception is thrown.

Make sure your path exists. I know you said you did, but check again.

Make sure you reach the actual File.Delete line

Cheers

I had the same issue before, follow the steps to see if it helps you. As silly as it may sound. Make sure no processes other than yours are making use of that file you created? Is this application multi-threaded? Do you have services running that use the file?

  1. Make sure your not in debug mode, and do a Build - Clean

  2. Check that the file is still at the location and copy the file path.

  3. Put a break point right before file deletion.

  4. Rebuild your project after you put your break-point .

  5. Press F5 to debug and step through the code.

  6. Check that the file path matches the one that you have.

I tried to reproduce similar situation with my own code

string fn = @"C:\Users\Public\Pictures\Sample Pictures\Desert - Copy.jpg";
//The file is locked by Image.FromFile
Image img = Image.FromFile(fn);
//If img.Dispose() here, then file is unlocked and can be deleted
if (File.Exists(fn))
{
    try
    {

        File.Delete(fn);
        Debug.WriteLine("file delete: " + fn);
    }
    catch
    {
        //Caught
        Debug.WriteLine("Could not delete: " + fn);
    }
}
else
{
    Debug.WriteLine("File doesn't exist: " + fn);
}

I found the file is locked by Image.FromFile() and therefore Could not delete. For the same reason, I believe your file with path fn is locked by the Filestream ftream

Can you try to do

fStream.Dispose();

before your delete process and rerun the program to see if you can delete the file? thanks. You can dispose the fstream and create a new one if needed,right?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM