Save and Write Out a Text File.

Chrism2

Registered User.
Local time
Today, 20:21
Joined
Jun 2, 2006
Messages
161
I've spent hours trying to get this right, but I'm making a monkey out of this: Any help would be great, because it sounds so easy!

I have a form with some Textboxes on it.

txtLegal
txtPhone
txtFax

I want to be able to save the contents of these textboxes to a .txt file.

I've been fiddling around for hours with Streamwriter, but I'm either getting an empty text file or a file called "1" with the info I need in it.

Heck, I'm lost. :-( Here's how far I have got....

Code:
Private Sub butExport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butExport.Click

        'Dialog to Save



        Dim legal As String
        Dim phone As String
        Dim fax As String
        Dim gfx As String
        Dim url As String
        Dim FileName As String
        Dim sw As System.IO.StreamWriter

        legal = Me.legal.Text
        phone = Me.phone.Text
        fax = Me.fax.Text
        gfx = Me.gfx.Text
        url = Me.url.Text



        With SaveFileDialog1
            .Filter = "Text files (*.txt)|*.txt|" & "All files|*.*"
            If .ShowDialog() = Windows.Forms.DialogResult.OK Then
                FileName = SaveFileDialog1.FileName



                sw.Write("legal =" & legal)
                sw.Write("phone =" & phone)
                sw.Write("fax =" & fax)
                sw.Write("gfx =" & gfx)
                sw.Write("url =" & url)

                sw.Close()

            End If

        End With

        Me.Close()

        frmEntry.Show()
    End Sub
End Class


Any help always appreciated!
 
Well I'm just entering into the .Net world but I wanna try my best to help ya:
First off, use filestream as well as streamwriter; gotta make sure the file exists first.
Also, don't define sw (Stream_Writer in my examples) so early, define it after the FileName as been determined and the FileStream has been created.
Code:
FileName = .Filename
dim Stream as New System.IO.FileStream(FileName)
dim Stream_Writer as New System.IO.StreamWriter(Stream)
Also, if you plan on writing to this file many times, may be best to move to the last line before writing your first new one.:
Code:
Stream_Writer.BaseStream.Seek(0, IO.SeekOrigin.End)
And using write line also adds a linefeed to the end of the line so rather than writing everything in the same line (unless you want to do that) use :
Code:
Stream_Writer.WriteLine("legal =" & legal)
Stream_Writer.WriteLine("phone =" & phone)
Stream_Writer.WriteLine("fax =" & fax)
Stream_Writer.WriteLine("gfx =" & gfx)
Stream_Writer.WriteLine("url =" & url)
Hope this helps; if not; read the disclaimer below! :D
It's currently working for me to run a log file, so hopefully it'll work for you.:)
 

Users who are viewing this thread

Back
Top Bottom