I'm writing a VB.NET 2010 application to take data from a text file, upload it to a SQL DB, then massage it prior to updating another database.
The data is coming from a UNIX system and I need to change the termination character to get Bulk Insert to work (don't ask me why, it's just the only way I've been able to get it to work).
I'm having a stupid problem with the clean up routine.
I use a streamreader to read the file and a streamwriter to write the changed data out to a temp file. That's working fine. Ultimately I then will delete the original file and rename the temp file.
Because I'm running it multiple times in testing I wanted to delete the temp file before I create a new one, but something in my code is holding a lock on it so that the routine gives a File in Use error and fails.
I can manually delete the file with no problems. Then I run the code and it works, but if I run it a second time it fails.
Here's the code:
Function ChangeTerminationChar(FI As FileInfo) As Boolean
Dim fs As New FileStream(FI.FullName, FileMode.Open)
Dim strFile As String = FI.DirectoryName & "\" & Left(FI.Name, Len(FI.Name) - 4) & ".tmp"
If File.Exists(strFile) Then
File.Delete(strFile) 'this line fails the second time I run the routine.
Dim fsr As New StreamReader(fs)
Using fstmp As New FileStream(strFile, FileMode.Create, FileAccess.ReadWrite)
Using fsw As New StreamWriter(fstmp)
Do Until fsr.Peek = -1
fsw.WriteLine(Replace(fsr.ReadLine(), Chr(10), Chr(13)))
Any ideas would be gratefully received as it's driving me crazy.