C#读取txt文件

本文提供了一个简单的C#代码片段,用于从服务器映射路径读取并解析TXT文件,将其内容转换为字符串数组。通过使用FileStream和StreamReader类,实现了文件的读取操作,并通过循环读取每一行数据,使用Split方法将其分解为关键信息。最后,代码展示了如何关闭资源以确保资源的正确释放。

public List<string> ReadtxtInfo()
{
FileStream fs = new FileStream(System.Web.HttpContext.Current.Server.MapPath("/") + @"/AllowList.txt", FileMode.Open);

StreamReader m_streamReader = new StreamReader(fs);
m_streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
List<string> Arrar1 = new List<string>();
string strLine = m_streamReader.ReadLine();
do
{
string[] split = strLine.Split('=');
string a = split[1];
Arrar1.Add(a);
strLine = m_streamReader.ReadLine();
} while (strLine != null && strLine != "");

m_streamReader.Close();
m_streamReader.Dispose();
fs.Close();
fs.Dispose();
return Arrar1;

}

转载于:https://www.cnblogs.com/zxk3113/p/5231958.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值