如何将Windows窗体GUI中的字段值保存到文件中,然后还原它们?

时间:2012-05-10 22:22:31

标签: c# windows forms streamreader streamwriter

我想将存储在文件中的内容加载到WinForms GUI的字段中。

我的方法现在:我有一个编写器,它将每个文本框写入.txt文件中的一行。我有streamreader设置,但我不知道如何让它将每行加载到单独的文本框中。例如:Task1_name(第1行)和task1_desc(第2行)需要在单独的文本框中,如何将其加载到框中?

由于

代码:

保存按钮:

void Save_buttonClick(object sender, EventArgs e)
{
    DialogResult dialogResult = MessageBox.Show("Do you want to save?", "Save", MessageBoxButtons.YesNo);
    if (dialogResult == DialogResult.Yes)
    {
        FileStream file = new FileStream("test.txt", FileMode.OpenOrCreate, FileAccess.Write);
        StreamWriter sw = new StreamWriter(file);
        sw.WriteLine(task1_name.Text);
        sw.WriteLine(task1_desc.Text);
        sw.WriteLine(task1_date.Value);
        sw.WriteLine(task1_check.Checked);
        sw.Close();
    }
}

加载按钮:

void Load_buttonClick(object sender, EventArgs e)
{
    DialogResult dialogResult = MessageBox.Show("Do you want to load?", "Load",     MessageBoxButtons.YesNo);
    if (dialogResult == DialogResult.Yes)
    {
        FileStream file = new FileStream("test.txt", FileMode.OpenOrCreate, FileAccess.Write);
        StreamReader sr = new StreamReader(file);
        sr.Close();
    }           
}

3 个答案:

答案 0 :(得分:2)

使用XML序列化。它看起来像这样:

public class MySettings 
{
  public String name {get;set;}
  public String name {get;set;}
  public DateTime date {get;set;}
  public bool checked {get;set;}
}

void Save() 
{
  var s = new MySettings 
  {
    name = this.task1_Name.Text,
    desc = this.task1_Desc.Text,
    date = this.task1_Date.Value,
    checked = this.task1_Check.Checked
  };
  var ser = new XmlSerializer(typeof(MySettings));
  using (var fs = new FileStream(path, FileMode.Create))
  {
      using (var tw = new StreamWriter(fs, new UTF8Encoding()))
      {
          var ns = new XmlSerializerNamespaces();
          ns.Add("", "");
          ser.Serialize(tw, this, ns);
      }
  }
}

加载它看起来像这样:

    static MySettings Load()
    {
        var ser = new XmlSerializer(typeof(MySettings));
        MySettings settings = null;

        try
        {
            using (var s = File.OpenRead(path))
            {
                settings = (MySettings) ser.Deserialize(s);
                // optionally validate here
            }
        }
        catch (Exception ex1)
        {
            MessageBox.Show("Cannot read settings. " + ex1.Message,
                            "error");
            settings = null;
        }
        return settings;
    }

然后当然重置您的表单将是这样的:

 var settings = Load();
 this.task1_Name.Text = settings.name;
 this.task1_Desc.Text = settings.desc;
 this.task1_Date.Value = settings.date;
 this.task1_Check.Checked = settings.checked;

答案 1 :(得分:0)

您可以将每行添加到List中并通过索引访问它们:

//...
List<string> lines = new List<string>();

using (StreamReader sr = new StreamReader(file))
{
    while(!sr.EndOfStream)
    {
        lines.Add(sr.ReadLine());
    }
}

task1_name.Text = lines[0];
task1_desc.Text = lines[1];
//...

答案 2 :(得分:0)

只需阅读这些行并按照您编写的顺序填充表单:

if (dialogResult == DialogResult.Yes)
{
    FileStream file = new FileStream("test.txt", FileMode.OpenOrCreate, FileAccess.Write);
    StreamReader sr = new StreamReader(file);
    task1_name.Text = sr.ReadLine();
    task1_desc.Text = sr.ReadLine();
    task1_date.Value = DateTime.Parse(sr.ReadLine());
    task1_checked.Checked = bool.Parse(sr.ReadLine());
    sr.Close();
}