none
How would i save text to a file?

    Question

  • Ok, heres the setup

    I am fooling around in Visual Basic trying to learn it. I have looked online in search engines like google trying to find how i can do this.

    I am creating a Application that will help running a Minecraft Server a little easier (they have applications i know but i would like to learn, and actually do something productive rather than create something that just says "hello world!". Minecraft has Server info saved to a file called "Server.Properties" which is opened in Notepad and can be edited.

    What i have created is a series of Checkboxes and Textboxes for user to input their info. I have it saving to Settings so that when they restart it, it has all their settings saved but how would i save this information into a format of

     

    Level-Name= "textbox1.text"
    
    Port Number="textbox2.text"
    
    Pvp="Checkbox.checked"

     

    I have found ways to append the text but that cant be loaded by the server.

    So Basic Question Is

    How do i save data so that there is non user input "LevelName=" then User Input "textbox1.text"

    and make it multilined?

    • Moved by lucy-liu Friday, July 15, 2011 8:32 AM it is related to vb (From:Visual Studio Editor)
    Wednesday, July 13, 2011 2:02 PM

Answers

  • Hey, Jag. You said you figured out how to append text, so here's a link on how to write text.
    http://msdn.microsoft.com/en-us/library/1t4kyezf(v=VS.100).aspx

    To modify an existing file (Server.Properties?), you have to pretty much re-write the whole file. You can't just change a line, even though a text file is a collection of text lines. Check out that link, and see if it helps.

     

    • Marked as answer by Liliane Teng Wednesday, July 20, 2011 3:18 PM
    Friday, July 15, 2011 9:11 AM
  • Question : How do i save data

    Answer : Use Application Settings to save data instead of text file ( easy and reliable ).

    Take a look at the link

    http://www.codeproject.com/KB/vb/appsettings2005.aspx

     

    After creating a setting you can read and set it like

     

    'Example for reading the saved value
    Textbox1.text = My.Settings.Name
    Textbox2.text = My.Settings.Port
    

    ' Example for set a value
    My.Settings.Name = textbox1.text
    


     

     

    • Marked as answer by Liliane Teng Wednesday, July 20, 2011 3:17 PM
    Friday, July 15, 2011 9:40 AM
  • The following shows you how to create a string that's formatted retrieving values from controls on a form and then putting a return at the end each line.  It then writes the value out (in this case, it doesn't append, but you could change that "False" to a "True" and it would append.

        Dim sb As New System.Text.StringBuilder
        sb.AppendFormat("LevelName={0}{1}", TextBox1.Text, vbCrLf)
        sb.AppendFormat("PortNumber={0}{1}", TextBox1.Text, vbCrLf)
        sb.AppendFormat("Pvp={0}{1}", CheckBox1.Checked, vbCrLf)
    
        My.Computer.FileSystem.WriteAllText("filename.txt", sb.ToString, False)
    
    

     

    Now, just to toss this out there, it looks like the format of the setinsg file is a standard INI format (which is variable=value).  There are Windows API's that will allow you to read/write to those values easily without you having to parse the field.  This would allow you to read their values in easily, and then also write them out easily without having to write the code to parse the file yourself.  Check this link out for some sample code on reading/writing from INI's.  It should get you going pretty quickly.

    http://www.vbknowledgebase.com/?Id=47&Desc=Read-Write-INI-files-using-VB-.Net

     

     

    • Marked as answer by Liliane Teng Wednesday, July 20, 2011 3:17 PM
    Friday, July 15, 2011 5:10 PM

All replies

  • Hi JAG171,

    This forum only discusses and askes questions about editor features in Visual Studio.

    This issue is about VB, I will move this thread from “Visual Studio Editor" forum to the “Visual Basic Genera” forum. There are more experts in the “Visual Basic Genera " forum.

    Thank you for your understanding!

     

    Best regards,

    Lucy


    Lucy Liu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, July 15, 2011 8:31 AM
  • Hey, Jag. You said you figured out how to append text, so here's a link on how to write text.
    http://msdn.microsoft.com/en-us/library/1t4kyezf(v=VS.100).aspx

    To modify an existing file (Server.Properties?), you have to pretty much re-write the whole file. You can't just change a line, even though a text file is a collection of text lines. Check out that link, and see if it helps.

     

    • Marked as answer by Liliane Teng Wednesday, July 20, 2011 3:18 PM
    Friday, July 15, 2011 9:11 AM
  • Question : How do i save data

    Answer : Use Application Settings to save data instead of text file ( easy and reliable ).

    Take a look at the link

    http://www.codeproject.com/KB/vb/appsettings2005.aspx

     

    After creating a setting you can read and set it like

     

    'Example for reading the saved value
    Textbox1.text = My.Settings.Name
    Textbox2.text = My.Settings.Port
    

    ' Example for set a value
    My.Settings.Name = textbox1.text
    


     

     

    • Marked as answer by Liliane Teng Wednesday, July 20, 2011 3:17 PM
    Friday, July 15, 2011 9:40 AM
  • The following shows you how to create a string that's formatted retrieving values from controls on a form and then putting a return at the end each line.  It then writes the value out (in this case, it doesn't append, but you could change that "False" to a "True" and it would append.

        Dim sb As New System.Text.StringBuilder
        sb.AppendFormat("LevelName={0}{1}", TextBox1.Text, vbCrLf)
        sb.AppendFormat("PortNumber={0}{1}", TextBox1.Text, vbCrLf)
        sb.AppendFormat("Pvp={0}{1}", CheckBox1.Checked, vbCrLf)
    
        My.Computer.FileSystem.WriteAllText("filename.txt", sb.ToString, False)
    
    

     

    Now, just to toss this out there, it looks like the format of the setinsg file is a standard INI format (which is variable=value).  There are Windows API's that will allow you to read/write to those values easily without you having to parse the field.  This would allow you to read their values in easily, and then also write them out easily without having to write the code to parse the file yourself.  Check this link out for some sample code on reading/writing from INI's.  It should get you going pretty quickly.

    http://www.vbknowledgebase.com/?Id=47&Desc=Read-Write-INI-files-using-VB-.Net

     

     

    • Marked as answer by Liliane Teng Wednesday, July 20, 2011 3:17 PM
    Friday, July 15, 2011 5:10 PM