How do I add Textbox1.Text to a textfile?

Recommended Answers

All 4 Replies

Use streamwriter

dim oWrite as system.io.streamwriter
'openfile
oWrite = system.io.file.opentext("C:\sample")
'write textbox1.text to the textfile
oWrite.WriteLine(textbox1.text)
'leave a space
oWrite.WriteLine()
'close file
oWrite.close()

Hope that helps :)
Cheers,

Use streamwriter

dim oWrite as system.io.streamwriter
'openfile
oWrite = system.io.file.opentext("C:\sample")
'write textbox1.text to the textfile
oWrite.WriteLine(textbox1.text)
'leave a space
oWrite.WriteLine()
'close file
oWrite.close()

Hope that helps :)
Cheers,

This line gets an error:
oWrite = system.io.file.opentext("C:\sample")
This is the error:
Error 1 Value of type 'System.IO.StreamReader' cannot be converted to 'System.IO.StreamWriter'. C:\Documents and Settings\nevets04\Local Settings\Application Data\Temporary Projects\Employee Manager\Form1.vb 12 18 Employee Manager

Using sw As New System.IO.StreamWriter("C:\Sample.txt")    'Opens/creates file & StreamWriter object
    sw.WriteLine(TextBox1.Text)    'Outputs textbox value
End Using    'Closes & disposes StreamWriter object

Yeah, my bad mate.
if you want to write new lines in the existing text file, here is the code.

Dim oWriter As systems.io.Streamwriter
        oWriter = systems.io.File.AppendText("c:\sample.txt")
        oWriter.WriteLine("abc first")
        oWriter.WriteLine("haha")
        oWriter.WriteLine()
        oWriter.Close()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.