title: CreateText method produces error "The process cannot access the file because it is being used by another process"
body: <p>I am working on a button that will delete text in gross.txt. I keep getting this error:</p>
  
  <blockquote>
    <p>The process cannot access the file 'C:\Users\isaiah\Visual BASIC Programs\VB2012\Chap10\Gross Pay Solution\Gross Pay Project\bin\Debug\gross.txt' because it is being used by another process.</p>
  </blockquote>
  
  <p>Or more specifically:</p>
  
  <blockquote>
    <p>A first chance exception of type 'System.IO.IOException' occurred in mscorlib.dll.</p>
  </blockquote>
  
  <p>It appears the error comes from btnDelete_Click at the bottom.</p>
  
  <pre><code>Option Explicit On
  Option Strict On
  Option Infer Off
  
  Public Class frmMain
  
      Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
          Me.Close()
      End Sub
  
      Private Sub txtGrossPay_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtGrossPay.KeyPress
  
          ' Allows the text box to accept only numbers, the period, and the Backspace key
          If (e.KeyChar &lt; "0" OrElse e.KeyChar &gt; "9") AndAlso e.KeyChar &lt;&gt; "." AndAlso e.KeyChar &lt;&gt; ControlChars.Back Then
              e.Handled = True
          End If
      End Sub
  
      Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
  
          Dim outFile As IO.StreamWriter
          outFile = IO.File.AppendText("gross.txt")
          outFile.WriteLine(txtGrossPay.Text)
          outFile.Close()
  
          txtGrossPay.Text = ""
      End Sub
  
      Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
  
          ' Declare new inFile Varable as a streamreader object
          Dim inFile As IO.StreamReader
          Dim dblLine As Double
  
          ' Opens gross.txt for input
          inFile = IO.File.OpenText("gross.txt")
  
          ' .Exists() searches /bin folder for gross.txt, returns a boolean value
          If IO.File.Exists("gross.txt") Then
              inFile = IO.File.OpenText("gross.txt")
  
              'Fill the list with the values
              Do Until inFile.Peek = -1
  
                  Double.TryParse(inFile.ReadLine, dblLine)
  
                  lstContents.Items.Add(dblLine.ToString("C2").PadLeft(6, " "c))
              Loop
          Else
              MessageBox.Show("The file you have requested does not exist", "Gross Pay Project",
                              MessageBoxButtons.OKCancel, MessageBoxIcon.Error)
          End If
  
          inFile.Close()
      End Sub
  
      Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
          Dim outDelete As IO.StreamWriter
          outDelete = IO.File.CreateText("gross.txt")
          outDelete.Close()
      End Sub
  End Class
  </code></pre>