我有一个托管的文本文件,这个文件有一个像这样的有组织的字符串:
- 第一行
- 第二行
- 三线
- 第四行
- 第六行
- 第七行
- ......................
我使用以下函数从该文件中获取所有内容:
private static List<string> lines;
private static string DownloadLines(string hostUrl)
{
var strContent = "";
var webRequest = WebRequest.Create(hostUrl);
using (var response = webRequest.GetResponse())
using (var content = response.GetResponseStream())
using (var reader = new StreamReader(content))
{
strContent = reader.ReadToEnd();
}
lines = new List<string>();
lines.Add(strContent);
return strContent;
}
// Button_click
DownloadLines("http://address.com/folder/file.txt");
for (int i = 0; i < lines.Count; i++)
{
lineAux = lines[0].ToString();
break;
}
Console.WriteLine(lineAux);
然后,我如何访问此方法返回的这个大型有组织字符串中的第一个索引(例如文本)?
最佳答案
这样可以逐行读取文本文件
private static List<string> DownloadLines(string hostUrl)
{
List<string> strContent = new List<string>();
var webRequest = WebRequest.Create(hostUrl);
using (var response = webRequest.GetResponse())
using (var content = response.GetResponseStream())
using (var reader = new StreamReader(content))
{
while (!reader.EndOfStream)
{
strContent.Add(reader.ReadLine());
}
}
return strContent;
}
从方法返回此列表后,您可以使用列表索引访问文本行
关于c# - 如何逐行读取 StreamReader 文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37103839/