Writing to File error!

masj78

Active member
Joined
Aug 8, 2003
Messages
31
Location
Harrogate, UK
I am using a file as a data store. It will allow me to write one word or sentance but if I try to write more than one it gives me my error opening file. Code is as follows:


VB.NET:

Dim strStore As String
Dim fileName As New IO.StreamWriter(File.Open("C:\store.txt", IO.FileMode.Append))
-----------------------------------------------------------------------------------

Private Sub cmdStore_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdStore.Click

Try

strStore = storeText.Text Text from text box
fileName.WriteLine(System.Environment.NewLine)
fileName.WriteLine(strStore) write to stream
MsgBox(strStore & " has been stored!") message has stored
fileName.Flush() flush to file
fileName.Close() close file
fileName = Nothing

Catch ex As Exception

MsgBox("Error Opening File!") error that is showing on second store attempt

End Try

End Sub


Have a vague idea its something to do with opening and closing the file.
Any ideas of the solution anyone!:confused:
 
Code:
fileName.WriteLine(System.Environment.NewLine)
fileName.WriteLine(strStore)  write to stream
The first of the above lines may be redundant, .WriteLine should write out the data and include an appropriate line terminator (e.g. CRLF)
 
I have debugged as suggested and the line causing the problem is :

fileName.WriteLine(System.Environment.NewLine)

The error is:

A first chance exception of type System.NullReferenceException occurred in WordMatcher.exe

Additional information: Object reference not set to an instance of an object.


If this line is commented out it gives the same error, but on the line below:

fileName.WriteLine(strStore)

Intead of closing the file after the word is added I have put it when the program is exited and it seems to have worked. Would like some explanation on the error, If possible!
 
try changing
Code:
Dim fileName As New IO.StreamWriter(File.Open("C:\store.txt", IO.FileMode.Append))

to
Code:
Dim fileName As New IO.StreamWriter(File.Open("C:\store.txt", IO.FileMode.Append, FileAccess.Write))
 
Changing/adding parameters seems to make no difference. It seems that you need to keep the file open, do all your editing, then close it. Doesnt like the file being constantly open and closed at run time it seems. Have learnt a lot so far and at least I have a solution.

cheers for al the help everyone!

For anyone interested, the solution I found was to add this command on the streamwriter on the exit button.

Private Sub cmdExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExit.Click
fileName.Close() close file
fileName = Nothing

End
End Sub
 
Or if you open the File at the start of the function and close it at the end - just realised you were opening it outside the function (soon as the form is loaded into memory by the look of it) and closing it when the function exits. The next time the function is called the file will be closed.
 
your best bet would be to build up the string you want to write, then use writeline in one go, eg:
Code:
strStore = "some stuff"
strStore += Environment.NewLine
strStore += "some more stuff"
fileName.WriteLine(strStore)

also, have you tried using just " Write " rather than " WriteLine "?
 
Back
Top