writing to a file problem!

masj78

Active member
Joined
Aug 8, 2003
Messages
31
Location
Harrogate, UK
I am taking input from a text box and trying to write to a blank .txt file I already have saved on my C:\ drive, with the following:



Dim fileName As New System.IO.StreamWriter("C:\store.txt", True)
Dim strStore As String
-----------------------------------------------------------------------------

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(strStore)
fileName.Close()



Catch ex As Exception

MsgBox("Error Opening File!")

End Try

End Sub


The store.txt file is still blank, What am I doing wrong?:confused:
 
you could always try adding the filestream to it, like this...
Code:
    Dim fileName As New IO.StreamWriter(New IO.FileStream("C:\store.txt", IO.FileMode.OpenOrCreate))
    Dim strStore As String


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        strStore = RichTextBox1.Text
        fileName.WriteLine(strStore)
        fileName.Close()
    End Sub
see if that helps, it works ok at this end.:)
 
Have tried as you suggested flushing the Stream contents to the file, as I can see is needed when I re-read up on StreamWriters.
I also added the Imports System.IO to general decs and edited the StreamWriter to:


Try
Dim fileName As New _
IO.StreamWriter(File.Open("C:\store.txt", FileMode.Open))

strStore = storeText.Text Text from text box
fileName.Write(strStore) write input to file
MsgBox(strStore & " has been stored!") message has stored
fileName.Flush()
fileName.Close() close file
fileName = Nothing

Catch ex As Exception

MsgBox("Error Opening File!")

End Try

File is still blank though and I dont know why. I have followed book advice but still not working. Ahhhhhhhhh!!!!!! Help!
 
Here is one of the functions I use to write text, and works every time!
C#:
public static void WriteText(string path, object o)
{
	StreamWriter Sw= new StreamWriter(path);
	try
	{
		Sw.Write(o);
	}
	catch{MessageBox.Show("Could not write file.", "Error");}
	Sw.Close();
}
Try removing FileMode.Open in your code as seen in mine. See if that works.
 
Will try your suggestion dynamic_sysop and report back
Cheers. This way is similar to reading to files in Java, which I have done a lot. I new it would end up being something like that.

Dim fileName As New IO.StreamWriter(New IO.FileStream("C:\store.txt", IO.FileMode.OpenOrCreate))

Here goes!

:-\
 
Seems to have worked fine. Maybe I was not waiting long enough to let it write.
Will only seem to allow one word to be added though and then causes an error on the second added.
If the program is ended and restarted it overwrites the single word in the file with the new one. Added a carriage return but no luck.
Looks like lots of work still to be done here. Have seen a another post somewhere about overwriting.
Thanks for the help so far everyone!
 
Back
Top