I am constructing my first VB.Net application using VB2010 Express. I have been away from programming for some years my last experience was with VB6.
My application is designed to download data from a web source and store it, line by line, in a simple text file. Access to the files is completely sequential, only one access to the save routine at a time, using a loop. A present there is only a Save routine because obviously theres no point writing a read routine until the files are actually saving.
Every time I try to test the application I get the same IO.Exception telling me that the file is being used by another process.
Below is all the code relating fo file creation and writing. Firstly the application checks that the folder is present, Then that the savefile required is present. If either is missing they are created. These sections appear to be working correctly as the folders and files are all created as expected. But when the application tries to write data to the file it has just created, it falls over. I realise I've probably made a stupid mistake but I can't see it. Can anyone help? Many thanks in advance.
Public Sub CheckFolderExists(ByVal strPath As String)
Dim di As New DirectoryInfo(strPath)
With di
If Not .Exists Then
.Create()
End If
End With
End Sub
Public Sub CheckFileExists(ByVal strPath As String)
Dim fi As New FileInfo(strPath)
With fi
If Not .Exists Then
.Create()
End If
End With
End Sub
Public Sub Save_Data(ByVal strPath As String, ByVal strData As String)
Using fs As New FileStream(strPath, FileMode.Append, FileAccess.Write), sw As New StreamWriter(fs)
sw.WriteLine(strData)
sw.Dispose()
fs.Dispose()
End Using
End Sub
My application is designed to download data from a web source and store it, line by line, in a simple text file. Access to the files is completely sequential, only one access to the save routine at a time, using a loop. A present there is only a Save routine because obviously theres no point writing a read routine until the files are actually saving.
Every time I try to test the application I get the same IO.Exception telling me that the file is being used by another process.
Below is all the code relating fo file creation and writing. Firstly the application checks that the folder is present, Then that the savefile required is present. If either is missing they are created. These sections appear to be working correctly as the folders and files are all created as expected. But when the application tries to write data to the file it has just created, it falls over. I realise I've probably made a stupid mistake but I can't see it. Can anyone help? Many thanks in advance.
Public Sub CheckFolderExists(ByVal strPath As String)
Dim di As New DirectoryInfo(strPath)
With di
If Not .Exists Then
.Create()
End If
End With
End Sub
Public Sub CheckFileExists(ByVal strPath As String)
Dim fi As New FileInfo(strPath)
With fi
If Not .Exists Then
.Create()
End If
End With
End Sub
Public Sub Save_Data(ByVal strPath As String, ByVal strData As String)
Using fs As New FileStream(strPath, FileMode.Append, FileAccess.Write), sw As New StreamWriter(fs)
sw.WriteLine(strData)
sw.Dispose()
fs.Dispose()
End Using
End Sub