Hi Guys,
I have got a problem. I the below stated quote.. I often keep getting the exception "The Process couldn't access the file because it is being used by another process." when it tries to delete the file.
please can anyone tell me where i am going wrong. The program may works OK for weeks or it may throws up this exception twice a week. Suggestions please!
Code:Private Sub UploaddatatoCAC_CORE() Dim dirinfo As DirectoryInfo Dim allFiles() As FileInfo dirinfo = New DirectoryInfo("E:\SQLUPDATE\CAC") allFiles = dirinfo.GetFiles("*.csv") Thread.Sleep(1000) If allFiles.Length <> 0 Then Try For Each fl As FileInfo In allFiles 'MsgBox(fl.FullName.ToString()) Dim con As SqlConnection = New SqlConnection(SQL_con2) Dim sr As StreamReader = New StreamReader(fl.FullName) Dim line As String = sr.ReadLine If String.IsNullOrEmpty(line) Then sr.Close() System.IO.File.Delete(fl.FullName) sr.Dispose() Continue For End If Dim value() As String = line.Split(Microsoft.VisualBasic.ChrW(44)) Dim dt As DataTable = New DataTable Dim row As DataRow For Each dc As String In value dt.Columns.Add(New DataColumn(dc)) Next While Not sr.EndOfStream value = sr.ReadLine.Split(Microsoft.VisualBasic.ChrW(44)) If (value.Length = dt.Columns.Count) Then row = dt.NewRow row.ItemArray = value dt.Rows.Add(row) End If End While Dim bc As SqlBulkCopy = New SqlBulkCopy(con.ConnectionString, SqlBulkCopyOptions.TableLock) bc.DestinationTableName = "[ANDONDB].[dbo].[CAC_LData]" bc.BatchSize = dt.Rows.Count con.Open() bc.WriteToServer(dt) bc.Close() con.Close() sr.Close() System.IO.File.Delete(fl.FullName) sr.Dispose() Next Catch ex As Exception MsgBox(ex.Message) End Try End If End Sub




Reply With Quote
