title: SSIS Script Task System.IO.File.WriteAllText throws "The process cannot access the file" Error
body: <p>I have been tasked with writing a SSIS package that automates the creation of a SQL Server database. During the process it needs to create a DSN file (text file) so that an Access tool can connect to the SQL Server database. This is all legacy, I cannot change the requirements. I also can't change the fact that I'm working in Visual Studio 2005 using VB.NET.</p>
  
  <p>The problem is, when I try to write to the DSN file I get an error: "The process cannot access the file '\Acmshares2\clntrial\DataMgt\DM_DSNs\DM_C001.dsn' because it is being used by another process." The file is being created, but I can't write to it. It's as if the line that creates the file is "holding on" to it somehow. Here's the code - note that the error gets thrown whether the line <code>System.IO.File.OpenWrite(sFilePath)</code> is there or not:</p>
  
  <pre><code>Public Sub Main()
      Dim sStudyID As String
      Dim sText As String
      Dim sFileName As String
      Dim sFilePath As String
  
      If Dts.Variables.Contains("StudyID") = True Then
          sStudyID = CType(Dts.Variables("StudyID").Value, String)
      End If
  
      sText = "[ODBC]" &amp; vbCrLf
      sText = sText &amp; "DRIVER=SQL Server" &amp; vbCrLf
      sText = sText &amp; "UID=DM" &amp; vbCrLf
      sText = sText &amp; "DATABASE=DM_" &amp; sStudyID &amp; vbCrLf
      sText = sText &amp; "WSID=ACMDM" &amp; vbCrLf
      sText = sText &amp; "APP=Microsoft Data Access Components" &amp; vbCrLf
      sText = sText &amp; "SERVER=ACMDM" &amp; vbCrLf
  
      sFileName = "DM_" &amp; sStudyID &amp; ".dsn"
      sFilePath = "\\Acmshares2\clntrial\DataMgt\DM_DSNs\" &amp; sFileName
  
      If Not System.IO.File.Exists(sFilePath) Then
          System.IO.File.Create(sFilePath)
      End If
      System.IO.File.OpenWrite(sFilePath)
      System.IO.File.WriteAllText(sFilePath, sText)
  
      Dts.TaskResult = Dts.Results.Success
  End Sub
  </code></pre>
  
  <p>Any help will be much appreciated!</p>