Simon Mourier's Avatar
Simon Mourier's blog (1807 answers on StackOverflow) about Microsoft technologies C#, C/C++, .NET, WinRT, WinUI3, AOT, P/Invoke, COM, Interop, DirectX, Direct2D, Windows, ...
  • 🔎︎ Search
  • Categories
  • Archives
  • About Me
  • .net
  • c#
  • storage

How do I properly open a file for deletion?

Aug 9, 2011 See the question and my original answer on StackOverflow

Something like this:

    using (FileStream file = new FileStream(path,
       FileMode.Open,
       FileAccess.ReadWrite,
       FileShare.Delete))
    {
        // you can read the file here and check your stuff
        File.Delete(path);
    }

PS: note the 'using' keyword. It allows you to have a cleaner code as it takes care of Dispose calls.

  • SmoBlog © 2010-2026 Simon Mourier. All rights reserved.
  • Privacy & Terms