vb.net 保存和加载数据的简单方法 Visual Basic
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10712407/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Simple way to save and load data Visual Basic
提问by Jonathan
I was wondering what is the easiest way to save and load data through different forms in vb. I just want to save 3 textbox.text that a user saves and be able to load it on a different form.
我想知道在 vb 中通过不同形式保存和加载数据的最简单方法是什么。我只想保存用户保存的 3 个 textbox.text 并能够将其加载到不同的表单上。
采纳答案by Steven Doggart
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.
但是,保存到根目录中的文件并不明智。最安全的做法是将其存储在独立存储中的文件中。此外,将它存储在 XML 中会更好。这可以通过序列化轻松完成。
回答by Mark Hall
If it is a User setting you can use the built in My.SettingsObject to Save and Load.
如果是用户设置,您可以使用内置的My.Settings对象来保存和加载。
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.
My.Settings 对象提供对应用程序设置的访问,并允许您动态存储和检索应用程序的属性设置和其他信息。
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>
回答by Doug Null
Complete description of solution, from Microsoft.
One greatthing about Microsoft has been fantastic documentation for at least the last 25 years. MSDN quality is on par even with Stackoverflow (software mecca)
至少在过去的 25 年中,Microsoft 的一大优点是出色的文档。MSDN 质量与 Stackoverflow(软件圣地)不相上下