title: UnauthorizedAccessException on creating file after deletion using FileStream
body: <p>I am facing an issue on client system. On trying to reproduce it in a sample code I have reproduced it.</p>
  
  <p>Here's the sample code</p>
  
  <pre><code>Imports System.IO
  
  Public Class Form1
  
      Private _lock As New Object
  
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
          Dim t As New Threading.Thread(AddressOf createFile)
          With t
              .IsBackground = True
              .Name = Guid.NewGuid.ToString
              .Start()
          End With
      End Sub
  
      Private Sub createFile()
          Dim path As String = "D:\SomeFile.txt"
  
          For i As Integer = 0 To 1000
              SyncLock _lock
                  If File.Exists(path) Then File.Delete(path)
  
                  Using fs As New FileStream(path, FileMode.CreateNew)
  
                  End Using
              End SyncLock    
          Next
      End Sub
  End Class
  </code></pre>
  
  <p>Just Run this code and click the button 3-4 times and notice the exception as shown in screenshot below:</p>
  
  <p><img src="https://i.stack.imgur.com/wz041.jpg" alt="enter image description here"></p>
  
  <p>The stacktrace of this exception is:</p>
  
  <blockquote>
    <p>System.UnauthorizedAccessException was unhandled   Message=Access to
    the path 'D:\SomeFile.txt' is denied.   Source=mscorlib   StackTrace:
           at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
           at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share,
    Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs,
    String msgPath, Boolean bFromProxy, Boolean useLongPath)
           at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions
    options, String msgPath, Boolean bFromProxy)
           at System.IO.FileStream..ctor(String path, FileMode mode)
           at WindowsApplication1.Form1.createFile() in C:\Users\premjeet.singh\Desktop\WindowsApplication1\WindowsApplication1\Form1.vb:line
    23
           at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
           at System.Threading.ExecutionContext.runTryCode(Object userData)
           at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode
    code, CleanupCode backoutCode, Object userData)
           at System.Threading.ExecutionContext.RunInternal(ExecutionContext
    executionContext, ContextCallback callback, Object state)
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean
    ignoreSyncCtx)
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
           at System.Threading.ThreadHelper.ThreadStart()   InnerException:</p>
  </blockquote>
  
  <p>Can anybody let me know the reason for this UnauthorizedAccessException exception as the file is already deleted before creating the new one and how it can be solved?</p>