以C锐格式保存和重新加载数据
本文关键字:加载 数据 新加载 格式 保存 | 更新日期: 2023-09-27 18:22:14
我正在寻找一个解决方案来保存和重新加载我在文本框中输入的内容或我在组合框中填写的内容。这是我目前拥有的代码,但我不能100%确定它是否正确。我只是发布我需要帮助的代码。如果你需要整批代码,我可以发布,但我认为这与我的要求无关。这就是我到目前为止所做的,我不确定它是否真的会保存键入的信息,我也完全不确定该在loadLastBtn方法中放入什么。我只想把它保存到内存中,即使文本框文本发生了变化,然后当我点击loatLastbtn时,我想能够从内存中检索到它。
private void submitBtn_Click(object sender, EventArgs e)
{
List<Members> lstMembers = new List<Members>();
if (string.IsNullOrEmpty(firstNameTxt.Text) || string.IsNullOrEmpty(lastNameTxt.Text)
|| string.IsNullOrEmpty(userNameTxt.Text) ||
string.IsNullOrEmpty(passwordTxt.Text) || string.IsNullOrEmpty(confPassTxt.Text)
|| string.IsNullOrEmpty(majorBox.Text) || string.IsNullOrEmpty(specialtyBox.Text))
{
MessageBox.Show("You must enter in all fields before moving forward");
}
else
{
//would this save my data to the array and how can I reload this?
Members m1 = new Members(firstNameTxt.Text, lastNameTxt.Text, userNameTxt.Text,
passwordTxt.Text, confPassTxt.Text, majorBox.Text,
specialtyBox.Text);
lstMembers.Add(m1);
}
}
private void loadLastBtn_Click(object sender, EventArgs e)
{
for(int i = 0; i < lstMembers.Count; i++)
{
firstNameTxt.Text = lstMembers[i].FirstName;
lastNameTxt.Text = lstMembers[i].LastName;
userNameTxt.Text = lstMembers[i].UserName;
passwordTxt.Text = lstMembers[i].Password;
confPassTxt.Text = lstMembers[i].ConfPassword;
majorBox.Text = lstMembers[i].Major;
specialtyBox.Text = lstMembers[i].Specialty;
}
}
这是我创建的用户对象。我只是不确定现在当我想保存和重新加载时如何调用它。
class Members
{
private string firstName;
private string lastName;
private string userName;
private string password;
private string confPassword;
private string major;
private string specialty;
public Members(string firstName, string lastName, string userName, string password,
string confPassword, string major, string specialty)
{
this.firstName = firstName;
this.lastName = lastName;
this.userName = userName;
this.password = password;
this.confPassword = confPassword;
this.major = major;
this.specialty = specialty;
}
public string FirstName
{
get
{
return firstName;
}
set
{
FirstName = firstName;
}
}
public string LastName
{
get
{
return lastName;
}
set
{
LastName = lastName;
}
}
public string UserName
{
get
{
return userName;
}
set
{
UserName = userName;
}
}
public string Password
{
get
{
return password;
}
set
{
Password = password;
}
}
public string ConfPassword
{
get
{
return confPassword;
}
set
{
ConfPassword = confPassword;
}
}
public string Major
{
get
{
return major;
}
set
{
Major = major;
}
}
public string Specialty
{
get
{
return specialty;
}
set
{
Specialty = specialty;
}
}
我建议创建一个"POCO"(普通的旧c#对象)或结构来保存数据。当您想保存它时,只需将它序列化到磁盘(使用MS序列化(此处有更多信息),或使用JSON序列化方法(此处有详细信息)
这样,你就不用担心用户可能输入了什么(也不用担心对数据进行消毒和确保他没有使用你的转义符)。
在没有任何硬实现的情况下,最常见的方法是用";"编写所有变量在文件中的它们之间。然后从文件中读取它,并在用户按下按钮时将其重新分配到表单元素中。查看此指南,了解如何写入文件:
https://msdn.microsoft.com/en-us/library/8bh11f1k.aspx
我举一个简单的例子:
例如,我的表单由用户名和密码TextFields组成。在用户输入并按下登录键后,我将它们保存到文件中,以便下次用户登录时,这种方法会保存它
public void save(){
String username = txtUsername.Text;
String password = txtPassword.Text;
File.WriteAllText(path, username + ";" + password);
}
public void load(){
String parameters = File.ReadAllText(path);
String[] splitted = parameters.split(';');
txtUsername.Text = splitted[0];
txtPassword.Text = splitted[1];
}