【问题标题】:winforms read from text file using arrays c#winforms使用数组c#从文本文件中读取
【发布时间】:2017-08-15 10:46:26
【问题描述】:

我正在做winforms,我正在尝试从文本文件中读取一些行,以便之后我可以在列表框中显示它。在我的 winforms 中,我正在记录活动,它正在写入文本文件。

当我重新启动程序时,它不显示行。这是我将活动写入显示的文本文件的代码:

` private void btnOK_Click(object sender, EventArgs e)
        {
            using (StreamWriter sw = new StreamWriter(User.ID + "activity.txt", true))
            {
                sw.Write(Environment.NewLine + txtName.Text + Environment.NewLine + "Type: " + txtType.Text + Environment.NewLine + "People: " + Convert.ToInt32(txtPeople.Text) + Environment.NewLine + dateTimePicker1.Value + Environment.NewLine);
                sw.Close();
            }
        }` 

当我在左侧列表框中打开程序并在右侧列表框中打开程序的详细信息时,我试图显示这些行。

我尝试了类似的方法,但程序重新启动时它不显示信息。

 using (StreamReader sr = new StreamReader(User.ID + "activity.txt"))
            {
                while (sr.Peek() != -1)
                {
                    evName = sr.ReadLine();
                    evType = sr.ReadLine();
                    evPeople = Convert.ToInt32(sr.ReadLine());
                    evDay = DateTime.Parse(sr.ReadLine());

                }
            }

【问题讨论】:

  • 您可以在Form_Load 事件中添加您的阅读代码,这样每当您启动程序时,它都会查看activity.txt。只要确保添加适当的错误处理。

标签: c# .net wpf winforms listbox


【解决方案1】:

这个怎么样?

string[] Lines = System.IO.File.ReadAllLines(path);

【讨论】:

    猜你喜欢
    • 2011-12-28
    • 1970-01-01
    • 1970-01-01
    • 2021-12-19
    • 1970-01-01
    • 2011-05-05
    • 2020-02-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多