masj78 Posted August 19, 2003 Posted August 19, 2003 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: Quote
*Experts* mutant Posted August 19, 2003 *Experts* Posted August 19, 2003 Try flushing the stream: fileName.Flush() Quote
Leaders dynamic_sysop Posted August 19, 2003 Leaders Posted August 19, 2003 you could always try adding the filestream to it, like this... 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.:) Quote
masj78 Posted August 19, 2003 Author Posted August 19, 2003 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 don't know why. I have followed book advice but still not working. Ahhhhhhhhh!!!!!! Help! Quote
aewarnick Posted August 19, 2003 Posted August 19, 2003 Here is one of the functions I use to write text, and works every time! 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. Quote C#
masj78 Posted August 19, 2003 Author Posted August 19, 2003 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! :-\ Quote
masj78 Posted August 19, 2003 Author Posted August 19, 2003 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! Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.