none
StreamWriter RRS feed

Answers

  • Hi Mansin,

    You can simply add a " " at the end of each element you write :

     

    Dim sw As New StreamWriter(cboschool.Text + ".txt", True)
       If Dir(cboschool.Text + ".txt") = "" Then
        System.IO.File.Create(cboschool.Text + ".txt")
       End If
       sw.Write("Firstname:" + txtboxfirstname.Text + " ")
       sw.Write("Lastname:" + txtboxlastname.Text + " ")
       sw.Write("Address:" + txtboxaddress.Text + " ")
       sw.Close() 
    
    

    EDIT : i suggest you to use the "using" instruction with the Append property of the StreamWriter set to True :

       Using sw As New StreamWriter(cboschool.Text, True)
          sw.Write("Firstname:" + txtboxfirstname.Text + " Lastname:" + txtboxlastname.Text + " Address:" + txtboxaddress.Text)
        End Using


    N'hésitez pas à poser des questions si un problème subsiste ou quelque chose n'est pas clair. Dans l'autre cas, veuillez indiquer que le problème est résolu. Cordialement. - Best Regards.
    • Marked as answer by Mansin Saturday, April 16, 2011 2:09 PM
    Saturday, April 16, 2011 11:44 AM

All replies

  • Hi Mansin,

    You can simply add a " " at the end of each element you write :

     

    Dim sw As New StreamWriter(cboschool.Text + ".txt", True)
       If Dir(cboschool.Text + ".txt") = "" Then
        System.IO.File.Create(cboschool.Text + ".txt")
       End If
       sw.Write("Firstname:" + txtboxfirstname.Text + " ")
       sw.Write("Lastname:" + txtboxlastname.Text + " ")
       sw.Write("Address:" + txtboxaddress.Text + " ")
       sw.Close() 
    
    

    EDIT : i suggest you to use the "using" instruction with the Append property of the StreamWriter set to True :

       Using sw As New StreamWriter(cboschool.Text, True)
          sw.Write("Firstname:" + txtboxfirstname.Text + " Lastname:" + txtboxlastname.Text + " Address:" + txtboxaddress.Text)
        End Using


    N'hésitez pas à poser des questions si un problème subsiste ou quelque chose n'est pas clair. Dans l'autre cas, veuillez indiquer que le problème est résolu. Cordialement. - Best Regards.
    • Marked as answer by Mansin Saturday, April 16, 2011 2:09 PM
    Saturday, April 16, 2011 11:44 AM
  • Hi,

    You can try this:

     sw.Write("Firstname:" + txtboxfirstname.Text & space(5) &)
        
     sw.Write("Lastname:" + txtboxlastname.Text & space(5) &)
     
     sw.Write("Address:" + txtboxaddress.Text)
    

    Saturday, April 16, 2011 11:49 AM

  • My preference would be to use the following style:
     
    sw.Write("Firstname: {0} Lastname: {1} Address: {2}", _
     txtboxfirstname.Text, txtboxlastname.Text, txtboxaddress.Text)

    --
    Mike
    Saturday, April 16, 2011 12:24 PM
  • Do not use + for concatenating strings - use & instead.  It will save a lot of problems later on.

    Are you sure you want these items separated by spaces?  Using a space will make it difficult to get them back from the file you are saving to, because there will also be spaces in the midlle of names or address lines.  If you use another character that could not be included in a name or addess, such as a tab, or even just an unusual character like ~, then you would be able to analyse the file to recover the data the next time the program is run.

    Or, since you are saving the name of each field as well as the data for that field, another option is to put each one on a separate line.
      Firstname:Test1
      Lastname:Test2
      Address:Test3
      Sex:Test4

    That way you know that each person starts with a Firstname: line and ends with a Sex: line, and reading the data back in is relatively simple.

    Saturday, April 16, 2011 12:47 PM
  • Saturday, April 16, 2011 1:05 PM