C# 讀取txt文件存儲在String數組中

首先創建一個數組變量,用於記錄數組 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");

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章