1

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

我现在的方法:我有一个流写入器,它将每个文本框写入 .txt 文件中的一行。我有流式阅读器设置,但我不知道如何让它将每一行加载到单独的文本框中。例如: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();
    }           
}
4

3 回答 3

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;
于 2012-05-10T22:38:54.440 回答
0

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

//...
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];
//...
于 2012-05-10T22:38:46.223 回答
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();
}
于 2012-05-10T22:42:11.553 回答