Simple way to save and load data Visual Basic

家住魔仙堡 提交于 2019-11-28 07:42:04

The simplest option would be to save them to a simple delimited text file. For instance, this would save the values in a pipe-delimited file:

File.WriteAllText("C:\Data.txt", String.Join("|", new String() {TextBox1.Text, TextBox2.Text, TextBox3.Text}))

And this would read it in:

Dim values() as String = File.ReadAllText("C:\Data.txt").Split("|"c)
TextBox1.Text = values(0)
TextBox2.Text = values(1)
TextBox3.Text = values(2)

However, it's not smart to save to a file in the root directory. The safest thing would be to store it in a file in Isolated Storage. Also, it would be even better to store it in XML. This could be easily done with serialization.

If it is a User setting you can use the built in My.Settings Object to Save and Load.

From above Link:

The My.Settings object provides access to the application's settings and allows you to dynamically store and retrieve property settings and other information for your application.

You can create the Setting in your Project Property's Settings Section:

Which you can access like this.

dim MyTemp as String = My.Settings.MySetting

and Save it like this

My.Settings.MySetting = "StringValue"
My.Settings.Save()

This will be persisted in your Config file like this:

<userSettings>
    <WindowsApplication11.My.MySettings>
        <setting name="MySetting" serializeAs="String">
            <value>TempValue</value>
        </setting>
    </WindowsApplication11.My.MySettings>
</userSettings>

Complete description of solution, from Microsoft.

One great thing about Microsoft has been fantastic documentation for at least the last 25 years. MSDN quality is on par even with Stackoverflow (software mecca)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!