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