Results 1 to 10 of 10

Thread: save text to text file in vb.net? new to it. none of my code works!

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Toronto, Ontario, Canada
    Posts
    275

    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!

  2. #2
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018
    Try this:

    WriteTextToFile("C:\whatever\FileLOC.txt", "hello")

    Parksie

  3. #3
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Thumbs up

    I have a list of the new syntax. And it includes the use of text files. Take a look
    ~Peter


  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Toronto, Ontario, Canada
    Posts
    275
    Ok.. i need textbox1.text to be saved to a text box..please help..new to vb.net!

  5. #5
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    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

  6. #6
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083

    this will works

    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

  7. #7
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    What if you don't want to Append though?

  8. #8
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    then It must be set = false

  9. #9
    Lively Member
    Join Date
    Oct 2002
    Posts
    67
    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)

  10. #10
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    the function would be direct solution. (I don't know if Iamthewalrus15's way work out)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width