C# 读取txt文本数据

发布时间 2023-06-06 17:34:14作者: KevinSteven

public static List<string> GetTxtInfo(string FilePath,ref string errMsg)
{
List<string> result = new List<string>();
string path = FilePath;
if (File.Exists(path))
{
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
StreamReader m_streamReader = new StreamReader(fs, System.Text.Encoding.Default);
m_streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
string strLine = m_streamReader.ReadLine();
do
{
try
{
if (!string.IsNullOrEmpty(strLine))
{
result.Add(strLine);
}
}
catch (Exception err) { errMsg = err.ToString(); break; }
strLine = m_streamReader.ReadLine();
}
while (strLine != null && strLine.Trim() != "");
m_streamReader.Close();
m_streamReader.Dispose();
fs.Close();
fs.Dispose();
}
return result;
}