在下面的代码中:
我只从一个文本文件中读取,但我想从一个文件夹中的多个文件中读取,然后分配变量并为每个文件创建一个新的约会实例?
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;
}
答案 0 :(得分:0)
要获取目录中的所有文件,请使用函数System.IO.Directory.GetFiles("directoryPath");
阅读此页面以获取文档
https://msdn.microsoft.com/en-us/library/system.io.directory(v=vs.110).aspx