Hi I keep getting this error on my production server but on my development environment i dont get the error.
I keep getting The Process cannot access the file because it is being used by another process
here is the code i am using
Dim di As New System.IO.DirectoryInfo(Me.Page.Server.MapPath("..\Collection\Data"))
Dim aryFi As System.IO.FileInfo() = di.GetFiles("*.txt")
Dim fi As System.IO.FileInfo
Dim sConnStr As String = ConfigurationManager.AppSettings("Database")
Dim cn As New System.Data.SqlClient.SqlConnection(sConnStr)
Dim cmdInsertInto As New System.Data.SqlClient.SqlCommand("", cn)
cmdInsertInto.CommandType = System.Data.CommandType.Text
cmdInsertInto.CommandTimeout = 0
Dim count As Integer = 0
Dim count2 As Integer = 0
For Each fi In aryFi
cn.Open()
Dim file As New FileStream(fi.FullName, FileMode.Open, FileAccess.Read)
Dim stream As New StreamReader(file)
Dim strReqLine As String = stream.ReadLine()
Dim fileStrings() As String = fi.Name.ToString.Split(("_").ToCharArray)
Dim FileClient As String = fileStrings(0)
Dim FileProduct As String = fileStrings(1)
Dim FilePostingDate As String = fileStrings(2).TrimEnd((".txt").ToCharArray)
While Not strReqLine Is Nothing
cmdInsertInto.CommandText = "INSERT INTO BulkImportHoldingData (ClientName,Product,PostingDate,BulkColumn) VALUES ('" & FileClient & "','" & FileProduct & "','" & FilePostingDate & "','" & strReqLine.Replace("'", "") & "')"
cmdInsertInto.ExecuteNonQuery()
strReqLine = stream.ReadLine()
count += 1
con.Open()
Cmd = New SqlCommand("Update Progress set cnt = '" & count & "'", con)
Cmd.ExecuteNonQuery()
con.Close()
End While
stream.Close()
fi.MoveTo(fi.FullName.TrimEnd((".txt").ToCharArray) & ".old")
count2 += 1
cn.Close()
Next
Any idea what is causing this error, it reads about 25 out of 30 files in the folder and then gives me the error, the file sizes range between 8kb and 193kb. Any ideas?