save text to text file in vb.net? new to it. none of my code works!
save text to text file in vb.net? new to it. none of my code works!
Try this:
WriteTextToFile("C:\whatever\FileLOC.txt", "hello")
I have a list of the new syntax. And it includes the use of text files. Take a look
Ok.. i need textbox1.text to be saved to a text box..please help..new to vb.net!
The way you open the file for write is a bit tricky. Not sure if this is the "right way" but this works for me:
Code:Try
fs = File.Open(strFileName, FileMode.CreateNew)
Catch ex As Exception
Try
fs = File.Open(strFileName, FileMode.Truncate)
Catch ex2 As Exception
MsgBox(ex.Message)
End Try
End Try
Function Save_Text_To_File(ByVal FilePath As String, ByVal FileContent As String, _
Optional ByVal Append As Boolean = False) As Boolean
Dim sw As System.IO.StreamWriter
Try
sw = New System.IO.StreamWriter(FilePath, Append)
sw.Write(FileContent)
Return True
Catch e As Exception
Return False
Finally
If Not sw Is Nothing Then sw.Close()
End Try
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Call Save_Text_To_File("c:\test.txt", TextBox1.Text, True)
End Sub
What if you don't want to Append though?
then It must be set = false
Would it be easiest to do this?
to copy the text box to a richtextbox and use savefile?
richtextbox1.text=textbox1.text
richtextbox1..SaveFile"c:\text.txt",RichTextBoxStreamType.PlainText)
the function would be direct solution. (I don't know if Iamthewalrus15's way work out)