Results 1 to 11 of 11

Thread: Write to path

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2012
    Posts
    4

    Write to path

    I'm trying to make a button called "Save" which saves all of the information entered into the textboxes as a .txt document. I'm very notice in this language and I don't know how to do it.


    IO.File.WriteAllText

    ^ That's what I have now.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Write to path

    What exactly do you not understand about the examples of File.WriteAllText that you found on the web? I just Googled and the first result was the MSDN documentation, which is what everyone should read first anyway.

    http://msdn.microsoft.com/en-us/library/ms143375.aspx

    That includes, amongst other information, a code example. Is there anything in that you're not clear about?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: Write to path

    try this:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
    4.         'save
    5.         IO.File.WriteAllLines("c:\example.txt", New String() {TextBox1.Text, TextBox2.Text, TextBox3.Text})
    6.     End Sub
    7.  
    8.     Private Sub btnOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpen.Click
    9.         'open
    10.         Dim lines() As String = IO.File.ReadAllLines("c:\example.txt")
    11.         If lines.Length <> 3 Then Return
    12.  
    13.         TextBox1.Text = lines(0)
    14.         TextBox2.Text = lines(1)
    15.         TextBox3.Text = lines(2)
    16.  
    17.     End Sub
    18.  
    19. End Class

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Write to path

    Ah, I missed that it was TextBoxes rather than TextBox. My apologies.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    New Member
    Join Date
    May 2012
    Posts
    4

    Re: Write to path

    The code works sort've. What would I put for c:\example.txt If I just want it to open up the save dialog box as a .txt document on anyones computer.

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: Write to path

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
    4.         'save
    5.         Dim sfd As New SaveFileDialog With { _
    6.         .Title = "Choose file to save to", _
    7.         .Filter = "TXT (*.txt)|*.txt|All Files (*.*)|*.*", _
    8.         .FilterIndex = 0}
    9.  
    10.         if sfd.showdialog = dialogresult.ok then
    11.             IO.File.WriteAllLines(sfd.filename, New String() {TextBox1.Text, TextBox2.Text, TextBox3.Text})
    12.         end if
    13.     End Sub
    14.  
    15.     Private Sub btnOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpen.Click
    16.         'open
    17.         Dim ofd As New OpenFileDialog With { _
    18.         .Title = "Choose file to open", _
    19.         .Filter = "TXT (*.txt)|*.txt|All Files (*.*)|*.*", _
    20.         .FilterIndex = 0}
    21.        
    22.         if ofd.showdialog = dialogresult.ok then
    23.             Dim lines() As String = IO.File.ReadAllLines(ofd.filename)
    24.             If lines.Length <> 3 Then Return
    25.  
    26.             TextBox1.Text = lines(0)
    27.             TextBox2.Text = lines(1)
    28.             TextBox3.Text = lines(2)
    29.         end if
    30.  
    31.     End Sub
    32.  
    33. End Class

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: Write to path

    Quote Originally Posted by Hilt View Post
    The code works sort've.
    it does everything you asked for in your original question...

  8. #8

    Thread Starter
    New Member
    Join Date
    May 2012
    Posts
    4

    Re: Write to path

    Quote Originally Posted by .paul. View Post
    it does everything you asked for in your original question...
    'open
    Dim lines() As String = IO.File.ReadAllLines("c:\example.txt")
    If lines.Length <> 3 Then Return

    TextBox1.Text = lines(0)
    TextBox2.Text = lines(1)
    TextBox3.Text = lines(2)


    What root path do I need to put?
    Last edited by Hilt; May 26th, 2012 at 09:10 AM.

  9. #9

    Thread Starter
    New Member
    Join Date
    May 2012
    Posts
    4

    Re: Write to path

    Still have no clue what I'm doing, can anyone give me some insight?

  10. #10
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: Write to path

    post #6 is the working code.

  11. #11
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: Write to path

    Quote Originally Posted by Hilt View Post
    'open
    Dim lines() As String = IO.File.ReadAllLines("c:\example.txt")
    If lines.Length <> 3 Then Return

    TextBox1.Text = lines(0)
    TextBox2.Text = lines(1)
    TextBox3.Text = lines(2)


    What root path do I need to put?
    You would have to put the path filename of the file you actually want to use. The example code works with the file Example.Txt located in the root of the c drive. Should be pretty clear what you need to do.

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