【发布时间】: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