C# 正在将文件读取到列表中

C# 正在将文件读取到列表中,c#,file.readalllines,C#,File.readalllines,我有一个这样的文本文件 a1,b1,30.04.2017 c1,d1,30.05.2017 我想将每一行添加到列表中,前两列将是字符串变量s1和s2,第三列将转换为显示日期的变量 我一点经验都没有,通常我会在这里寻找答案。我只能找到包含变量名称和值的行的示例 我想我需要使用下面的代码 List<string> fileLines = new List<string>(); using (var reader = new StreamReader(fileName))

我有一个这样的文本文件

a1,b1,30.04.2017
c1,d1,30.05.2017
我想将每一行添加到列表中,前两列将是字符串变量s1和s2,第三列将转换为显示日期的变量

我一点经验都没有,通常我会在这里寻找答案。我只能找到包含变量名称和值的行的示例

我想我需要使用下面的代码

 List<string> fileLines = new List<string>();

using (var reader = new StreamReader(fileName))
{
    string line;
    while ((line = r.ReadLine()) != null)
    {

    }
}
List fileLines=new List();
使用(var reader=newstreamreader(文件名))
{
弦线;
而((line=r.ReadLine())!=null)
{
}
}
我不能做括号内的事情,即, 解析行, 将第一列赋值给变量s1 将第二列赋给变量s2 将第三个转换为日期并将其分配给d1 然后将行添加到列表中

提前感谢

简单一点

var text=File.ReadAllText(@“[FILEPATH]”)

var fileLines=text.Split(新字符串[]{Environment.NewLine},StringSplitOptions.RemoveEmptyEntries.ToList()

不过,这本可以很容易地用谷歌搜索到

以防万一,如果你需要完整的代码:这是我的版本

private static void TestReader()
    {
        var text = File.ReadAllText(@"D:\sample.txt");
        var fileLines = new List<SampleData>();
        foreach (var fileLine in text.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries))
        {
            var lineData = fileLine.Split(',');

            // Make sure all the keys are present / do a separate check
            if (lineData.Length <= 2)
            {
                continue;
            }

            fileLines.Add(new SampleData
            {
                Code = Convert.ToString(lineData[0]),
                Description = Convert.ToString(lineData[1]),
                SelectedDate = lineData.Length >= 3 ? DateTime.ParseExact(lineData[2], "dd.MM.yyyy", null) : DateTime.MinValue
            });
        }
    }

    public class SampleData
    {
        public string Code { get; set; }
        public string Description { get; set; }
        public DateTime SelectedDate { get; set; }
    }
private static void TestReader()
{
var text=File.ReadAllText(@“D:\sample.txt”);
var fileLines=新列表();
foreach(text.Split中的var fileLine(新字符串[]{Environment.NewLine},StringSplitOptions.RemoveEmptyEntries))
{
var lineData=fileLine.Split(',');
//确保所有钥匙都在/进行单独检查
如果(lineData.Length=3?DateTime.ParseExact(lineData[2],“dd.MM.yyyy”,null):DateTime.MinValue
});
}
}
公共类抽样数据
{
公共字符串代码{get;set;}
公共字符串说明{get;set;}
公共日期时间SelectedDate{get;set;}
}

在下面的代码中,我编写了解析后的数据,您可以根据需要使用它们:

List<string> fileLines = new List<string>();
using (var reader = new StreamReader(fileName))
        {
            string line;
            while ((line = reader.ReadLine()) != null)
            {
                if (line == "") continue; // refuse bare lines...
                string a = line.Split(',')[0];
                string b = line.Split(',')[1];
                DateTime dt = DateTime.ParseExact(line.Split(',')[2], "dd.MM.yyyy", null);
                fileLines.Add(line); // if you want....
            }
        }
List fileLines=new List();
使用(var reader=newstreamreader(文件名))
{
弦线;
而((line=reader.ReadLine())!=null)
{
如果(行==“”)继续;//拒绝裸行。。。
字符串a=line.Split(',')[0];
字符串b=line.Split(',')[1];
DateTime dt=DateTime.ParseExact(line.Split(',)[2],“dd.MM.yyyy”,null);
fileLines.Add(line);//如果需要。。。。
}
}
使用Linq:

var query = from line in File.ReadAllLines(filename)
where !string.IsNullOrEmpty(line)
let words = line.Split(";")
select new { Item1 = words[0], Item2 = words[1], Date = DateTime.ParseExact(words[2], "dd.MM.yyyy", null) };

谢谢你的回答。如何将该行添加到列表中?最后一行
fileLines.add(line)这样做。我应该事先声明列表吗?like var fileLines=new[];你的问题中有一个问题<代码>列表文件行=新列表()我没有在我的回答中重复这一点。很抱歉,我错过了它。我在“列表”中使用的术语是否有误?列表可以有多个列和行吗