Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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:

  • Leaders
Posted

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.:)

Posted

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!

Posted

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.

C#
Posted

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!

 

:-\

Posted

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!

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...