C#保存和加载数据 [英] C# Save And Load Data

查看:106
本文介绍了C#保存和加载数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用系统资源和字符串在C#中保存和加载数据?例如,我单击一个按钮,使标签显示"WoW is Fun!".另一个说没有Mincraft更好",另一个按钮将在程序启动时保存标签文本的数据,以备下次使用.有人可以帮忙吗?

How do i save and load data in C# using the system recources and strings?, eg, I click a button that makes a label says "WoW is Fun!" and another that says "No mincraft is better" and another button that will save the data of the labels text for next time when the program is launched. can anyone please help?

推荐答案

只要看看 C# Visual Studio中的设置. [^ ]链接.

希望对您有所帮助.
Just take a look at C# Settings.settings in Visual Studio[^] link.

I hope this will help you well.


如果标签只有几个,则可以使用每个应用程序都可以轻松访问的属性设置.右键单击Visual Studio中的应用程序,然后转到属性"选项卡,添加一个新属性并为其命名,例如"Label1".同样,为每个标签创建属性.然后在代码中保存标签,如下所示:

Properties.Settings.Default.Label1.Value = Label1.Text;
Properties.Settings.Default.Label2.Value = Label2.Text;

Properties.Settings.Save();

加载属性同样简单:

Label1.Text = Properties.Settings.Default.Label1.Value;
Label2.Text = Properties.Settings.Default.Label2.Value;



如果标签的数量可变,则可以将它们连接到一个列表中,如下所示:

字符串myLabelList = Label1.Text +"|" + Label2.Text +"|" +等.

然后,您可以将大字符串保存到在属性"中创建的字段中,就像上面一样.加载时,您必须拆分字符串.您可以为此使用string的Split()方法.


如果要保存很多标签,则可以考虑将它们添加到DataSet中,然后序列化数据集.

If you have only a few labels, you can use the property settings that are easily accessible from every application. Right click the application in Visual Studio and go to the Properties tab, add a new property and give it a name, say "Label1". Similarly, create properties for each of your labels. Then in code, you save the labels, like this:

Properties.Settings.Default.Label1.Value = Label1.Text;
Properties.Settings.Default.Label2.Value = Label2.Text;
etc.
Properties.Settings.Save();

To load properties is just as easy:

Label1.Text = Properties.Settings.Default.Label1.Value;
Label2.Text = Properties.Settings.Default.Label2.Value;
etc.


If you have a variable number of labels, then you could concatenate them into a list, like this:

string myLabelList = Label1.Text + "|" + Label2.Text + "|" + etc.

Then you can save that big string to a field you created in Properties, just like above. When you load it you would have to split the string. You can use the Split() method of string for this.


If you have lots of labels to save, then you might consider adding them to a DataSet then serializing the dataset.

using System.Data;

DataSet d = new DataSet();
DataTable t = new DataTable();
d.Tables.Add(t);
t.Columns.Add(new DataColumn("Labels", typeof(string)));
t.Rows.Add("asdf");
t.Rows.Add("asdfa");
d.WriteXml("MySavedLabels.xml");



然后,以后您可以像这样检索保存的标签:



Then later you can retrieve the saved labels like this:

DataSet d = new DataSet();
d.ReadXml("MySavedLabels.xml");
foreach (DataRow row in d.Tables[0].Rows)
{
    string myLabel = (string)row["MyLabel"];
    (do something here with the retrieved label myLabel);
}


按照此处说明的步骤操作:
follow the steps explained here: http://msdn.microsoft.com/en-us/library/aa730869(v=vs.80).aspx[^]


这篇关于C#保存和加载数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆