title: VB.NET - Problems trying to write a textfile
body: <p>I have problems when trying to delete/create/write to this file.</p>
  
  <p><strong>The error:</strong></p>
  
  <pre><code>The process cannot access the file 'C:\Users\Administrador\AppData\Local\Temp\PlayList_temp.txt' because it is being used by another process.
  </code></pre>
  
  <p><strong>The highlighted lines by debugger:</strong></p>
  
  <pre><code>If System.IO.File.Exists(Temp_file) = True Then System.IO.File.Delete(Temp_file)
  System.IO.File.Create(Temp_file)
  </code></pre>
  
  <p>(Any of two, if I delete one line, the other line takes the same error id)</p>
  
  <p><strong>The sub:</strong></p>
  
  <pre><code>Public Sub C1Button2_Click(sender As Object, e As EventArgs) Handles Button1.Click
  
          If Not playerargs = Nothing Then
  
          Dim Str As String
          Dim Pattern As String = ControlChars.Quote
          Dim ArgsArray() As String
          Dim Temp_file As String = System.IO.Path.GetTempPath &amp; "\PlayList_temp.txt"
          Dim objWriter As New System.IO.StreamWriter(Temp_file)
  
          Str = Replace(playerargs, " " &amp; ControlChars.Quote, "")
          ArgsArray = Split(Str, Pattern)
  
          If System.IO.File.Exists(Temp_file) = True Then System.IO.File.Delete(Temp_file)
          System.IO.File.Create(Temp_file)
  
      For Each folder In ArgsArray
          If Not folder = Nothing Then
              Dim di As New IO.DirectoryInfo(folder)
              Dim files As IO.FileInfo() = di.GetFiles("*")
                  Dim file As IO.FileInfo
  
              For Each file In files
                  ' command to writleline
                  'Console.WriteLine("File Name: {0}", file.Name)
                  'Console.WriteLine("File Full Name: {0}", file.FullName)
                  objWriter.Write(file.FullName)
                  objWriter.Close()
              Next
          End If
          Next
  
      If randomize.Checked = True Then
              RandomiseFile(Temp_file)
      End If
  
      Process.Start(userSelectedPlayerFilePath, playerargs)
      If autoclose.Checked = True Then
          Me.Close()
      End If
      Else
      MessageBox.Show("You must select at least one folder...", My.Settings.APPName)
      End If
  End Sub
  </code></pre>