c# 讀取txt文件並分隔


 public static List<PostPerson> GetNameByFile()
        {
            
            #region 讀取txt文件
            var file = File.Open(Environment.CurrentDirectory + "\\aaa.txt",  FileMode.Open);
            List<string> txt = new List<string>();
            using (var stream = new StreamReader(file, System.Text.Encoding.GetEncoding("gb2312")))
            {
                while (!stream.EndOfStream)
                {
                    txt.Add(stream.ReadLine());
                }
            }
            file.Close();
            List<PostPerson> models = new List<PostPerson>();
            //循環list
            foreach (string item in txt)
            {
                string[] tempstr = item.Split(',');
                PostPerson model = new PostPerson();
                model.pcode = tempstr[0];
                model.pname= tempstr[1];
                models.Add(model);
            }
            // var line = 0;
            //txt.ForEach(t => {
            //    var row = 0;
            //    t.Split(',').ToList().ForEach(p => {
            //        PostPerson model = new PostPerson();
            //        model.pcode = p;
            //        row++;
            //    });
            //    line++;
            //});
            return models;
            #endregion
        }

txt文件中的格式:

xxxxxx,xxxx
xxxxxx,xxxx

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM