form won't save...?

K

karim

Hello,
I have the simple code below to save text, but when I click the save btn
the form creates the file in the C drive, but it would not save what's in the
text box.

Dim sw As New IO.StreamWriter("C:\SomeFile.txt", True)
sw.WriteLine(txt1.Text)

any suggestions on why it's not saving be great.
thanks for your help...
 
C

Cor Ligthert[MVP]

Hi Karim,

The fact is that you are not closing the streamreader, that is implicitely
done by this code.
\\\
Using sw As New IO.StreamWriter("C:\SomeFile.txt", True)
sw.WriteLine(txt1.Text)
End Using
///

Cor
 
H

Herfried K. Wagner [MVP]

karim said:
I have the simple code below to save text, but when I click the save
btn
the form creates the file in the C drive, but it would not save what's in
the
text box.

Dim sw As New IO.StreamWriter("C:\SomeFile.txt", True)
sw.WriteLine(txt1.Text)

any suggestions on why it's not saving be great.
thanks for your help...

The stream writer uses a buffer. You can either flush the buffer by calling
its 'Flush' method or by setting the 'AutoFlush' property. Closing the
stream writer will flush the buffer and persist the text to disk too (see
Cor's reply).
 
K

kimiraikkonen

Hello,
    I have the simple code below to save text, but when I click the save btn
the form creates the file in the C drive, but it would not save what's inthe
text box.

Dim sw As New IO.StreamWriter("C:\SomeFile.txt", True)
        sw.WriteLine(txt1.Text)

any suggestions on why it's not saving be great.
thanks for your help...

Hi Karim,
As you can use StreamWriter with Using statement, you can shorten your
code by directly calling WriteAllText method as an alternative under
My namespace.

My.Computer.FileSystem.WriteAllText("C:\SomeFile.txt", _
txt1.Text, True)

http://msdn.microsoft.com/en-us/library/27t17sxs(VS.80).aspx

Also aware that UTF-8 is default in that method unless you specify
other encoding as 4th argument.

Hope this helps,

Onur Güzel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top