How is it possible that after calling File.Delete file still exists? I used simple code to reproduce the problem using File.Open . The expected exception is a FileNotFoundException . I checked the operation in Process Monitor v3.05, and the result for the file is “DELETE PENDING” and throws a UnauthorizedAccessException . Does anyone have an explanation?
public class Program { private const string DummyFileName = "dummy.txt"; private static void Main(string[] args) { int attempt = 0; while (true) { using (File.Create(DummyFileName)) { } File.Delete(DummyFileName); try { attempt++; using (File.Open(DummyFileName, FileMode.Open, FileAccess.ReadWrite, FileShare.None)) { } } catch (FileNotFoundException) { } catch (UnauthorizedAccessException ex) { Console.WriteLine("File exists{0}", File.Exists(DummyFileName)); Console.WriteLine("File remains in DELETE PENDING state in attempt {0}.", attempt); Console.WriteLine(ex); Console.ReadKey(); } } } }
source share