【问题标题】:I want to read text files from a folder?我想从文件夹中读取文本文件?
【发布时间】:2015-04-10 18:21:18
【问题描述】:

在下面的代码中:

我只读取一个文本文件,但我想读取一个文件夹中的多个文件,然后分配变量并为每个文件创建一个新的约会实例?

public bool Load()
    {
        DateTime start  = new DateTime(2000,01,01);
        int length = 0;
        string screenDiscription = "";
        string line;
        int i = 1;
        StreamReader sr = new StreamReader("Appointments.txt");

        if (!File.Exists("Appointments.txt"))
        {
            return true;
        }

        while ((line = sr.ReadLine()) != null)
        {
            if (i % 4 == 1)
            {
                start = DateTime.ParseExact(line, "dd/MM/yyyy HHmm", CultureInfo.CreateSpecificCulture("en-GB"));
            }
            if (i % 4 == 2)
            {
                length = int.Parse(line);
            }
            if (i % 4 == 3)
            {
                screenDiscription = line;
                apps.Add(new Appointment(start, length, screenDiscription));
            }
            i++;
    }
            sr.Close();
            return true;

    }

【问题讨论】:

  • 您考虑过使用数据库吗?您可以 xopy 将其中一些与您的应用程序一起部署。 SQLite 很流行,我认为 SQLServer 精简版也可以这样工作。
  • 另外,您可以考虑使用 json 之类的格式,您可以在其中轻松地将多个 xomplex 记录存储在单个文件中。如果整个数据都适合 RAM 并且您没有多个并发写入者,这很好。

标签: c# winforms text-files instance


【解决方案1】:

要获取目录中的所有文件,请使用函数System.IO.Directory.GetFiles("directoryPath"); 阅读此页面以获取文档 https://msdn.microsoft.com/en-us/library/system.io.directory(v=vs.110).aspx

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-27
    • 1970-01-01
    • 2015-02-09
    • 2011-10-11
    相关资源
    最近更新 更多