- 第一种:
txt有多少行就会返回长度为多少的List<string>
/// <summary>
/// 读取txt文件内容
/// </summary>
/// <param name="Path">文件地址</param>
public static List<string> ReadTxtContent(string txtPath)
{
string Path = txtPath;
StreamReader sr = new StreamReader(Path, Encoding.Default);
string content;
List<string> vs = new List<string>();
while ((content = sr.ReadLine()) != null)
{
vs.Add(content);
}
sr.Close();
return vs;
}
- 第二种:
返回txt的全部字符串
/// <summary>
/// 读取txt文件内容
/// </summary>
/// <param name="Path">文件地址</param>
public static string ReadTxtContent(string txtPath)
{
string Path = txtPath;
StreamReader sr = new StreamReader(Path, Encoding.Default);
string content;
string res="";
while ((content = sr.ReadLine()) != null)
{
res+=content+"\n";
}
sr.Close();
return res;
}