C# 读取txt文件存储在String数组中

来源:互联网 发布:淘宝学生证怎么搜 编辑:程序博客网 时间:2024/05/16 11:03

首先创建一个数组变量,用于记录数组 string[] Y_headname;

然后写读取文本文件的函数:

 //读取文本文件转换为array 
        public void Y_ReadTextFileToList(string fileName)
        {

           
//Encoding.Default为读取的编码方式

     StreamReader sr = new StreamReader(fileName, Encoding.Default);
           

     string content = sr.ReadToEnd();
           
//以回车为分隔符,分割读取的文本

     Y_headname = content.Split(new string[] { "\r\n" }, StringSplitOptions.None);
            
        }
 

最后调用函数即可:

//读取数据,System.Windows.Forms.Application.StartupPath为本地路径
       
Y_ReadTextFileToList(System.Windows.Forms.Application.StartupPath + @"\Data\source\数据.txt");

0 0
原创粉丝点击