C#读取txt文件

来源:互联网 发布:crossover mac破解 编辑:程序博客网 时间:2024/06/04 19:35

TxtDataService

class TxtDataService{    public List<Student> GetAllStudent()    {        List<Student> studentList = new List<Student>();        string[] temp = System.IO.File.ReadAllLines("Data/Data.txt",         System.Text.Encoding.Default);        for (int i = 0; i < temp.Length; i++)        {            string[] tp = temp[i].Split(',');            studentList.Add(                new Student()                {                    ID = int.Parse(tp[0]),                    Name = tp[1],                    Sex=tp[2],                    Age= int.Parse(tp[3]),                    Score= int.Parse(tp[4])                });        }        return studentList;    }}

Student.cs

public class Student{    public int ID { get; set; }    public string Name { get; set; }    public string Sex { get; set; }    public int Age { get; set; }    public int Score { get; set; }}

Data.txt

将文件属性设置为始终复制、内容

1111,赵一,女,1,112222,钱二,男,2,223333,孙三,男,3,334444,李四,男,4,445555,周五,女,5,556666,吴六,男,6,667777,郑七,女,7,778888,王八,男,8,88

源代码下载

0 0
原创粉丝点击