1

The goal is to replace a PDF-File which is currently saved on disk.

I am deleting the current PDF file from disk, then recreating a new one. This works fine unless the PDF is currently opened in the Microsoft Edge Browser.

// Try delete PDF-File (which is opened in Edge Browser)
var info = new FileInfo(pathToPdf);
if (info.Exists)
{
    try
    {
        info.Delete();
        // Same thing with the File.Delete call
        //File.Delete(path);

        Console.WriteLine("Success.");
    }
    catch (Exception)
    {
        Console.WriteLine("Failed.");
        return;
    }        
}

We get a "Success" print out even though the file is opened in Edge. If it were opened in Adobe Reader it would throw an exception (File in use).

Let's create a new file. (For demonstration purposes a text file with a .pdf ending)

try
{
    using (var writer = File.CreateText(pathToPdf))
    {
        writer.Write("Foo");
        writer.Flush();
        Console.WriteLine("Success.");
    }
}
catch (Exception e)
{
    Console.WriteLine("Failed.");
    return;
}    

I expected to be able to create a new file, since the Delete() didn't fail. Yet I get an UnauthorizedAccessException: "Access to the path 'XYZ' is denied."

As a workaround I can recheck if the file exists after deleting it.

var newInfo = new FileInfo(pathToPdf);
if (newInfo.Exists)
    // Delete failed

But why would I need to do this? Shouldn't FileInfo.Delete() or File.Delete(path) fail in the first place?

Notes:

  • Tested on Windows 10 Pro with .Net Framework 4.5.1

  • The file is still visible in the File-Explorer with its original filesize after it was deleted by code (while opened in Edge).

  • When closing the Edge Browser after deleting the file by code, the file vanishes from the File-Explorer and I can create a new file programatically.

  • This problem occurs only with PDFs being opened in Edge. When using a Text-File instead the Text-File gets deleted properly.

Any clarification and help is appreciated.

Best Chris

  • Your success message is outside of the try/catch block. Therefore it will always say success. Place it at the end of your try block. – ThePerplexedOne Sep 20 '16 at 12:19
  • Not true since I am returning if we fail. I am editing the code though. – SteppenWolf Sep 20 '16 at 12:20
  • I tested deleting a pdf file opened in edge, effectively the `File.Delete` method doesn't throw exception. But after deleting the file, if i try reload it edge says it cannot find the file and it was deleted. – Alessandro D'Andria Sep 20 '16 at 12:26
  • probably permission related, try doing info.IsReadOnly = false; before the exists if clause – Innat3 Sep 20 '16 at 12:32
  • Same behaviour that I described above. I am curious why the Delete doesnt fail. It normally does if files are opened in a software which keeps a lock on it. Since Edge doesnt seem to, why am I not allowed / able to rewrite the file? I need to recheck if the File.Exists after the File.Delete call to make sure that it isnt opened in Edge right now. If it is read only (which it is not, just checked), why am I allowed to delete it? – SteppenWolf Sep 20 '16 at 12:34
  • For what I can see the file is deleted even if you keep open in Edge and can see the icon (I created the pdf file in desktop opened with edge and deleted with a console app calling `File.Delete`). – Alessandro D'Andria Sep 20 '16 at 12:36
  • If I use [SHFileOperation](https://msdn.microsoft.com/en-us/library/windows/desktop/bb762164(v=vs.85).aspx) to move file to recycle bin, the file is still visible in Edge but the icon disappear as expected. – Alessandro D'Andria Sep 20 '16 at 12:42
  • Well this mean that `File.Delete` has no reason to throw exception, so it's behavior is correct i.e. the file is deleted. – Alessandro D'Andria Sep 20 '16 at 12:43
  • The question then is, why am I not allowed to recreate the file and furthermore, why does the `FileInfo.Exists` property return true after delete? – SteppenWolf Sep 20 '16 at 12:53
  • Sorry but I have no answer, but it's very strange behavior. Only thing I can suggest it's using `SHFileOperation` that remove the file effectively. – Alessandro D'Andria Sep 20 '16 at 12:56
  • Thanks a lot anyways! I have a workaround for now, but I want to understand why this is happening. Will edit my question when I have time with your insight. – SteppenWolf Sep 20 '16 at 13:00

2 Answers2

0

If the file does not exist, FileInfo.Delete() does nothing.

From msdn

WinNt4Family

Delete does not delete a file that is open for normal I/O or a file that is memory-mapped.

You get an UnauthorizedAccessException when the path is a directory.

raven
  • 2,381
  • 2
  • 20
  • 44
0

If FILE_SHARE_DELETE is set on the handle by Edge, then File.delete() can be called with success by another process even when the handle exists. The file is then marked for deletion and deleted after the handle is closed. Until then, it is still visible in the Explorer, but not accessible anymore.

For a more detailed explanation, see this SO post:

Odd behaviour when deleting Files with Files.delete()

Community
  • 1
  • 1
MaSiMan
  • 655
  • 6
  • 16