如何逐行读取 StreamReader 文本
How to read StreamReader text line by line
我托管了一个文本文件,该文件的组织字符串如下:
- 第一行
- 第二行
- 第三行
- 第四行
- 第六行
- 第七行
- .......
我正在使用以下函数从此文件中获取所有内容:
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;
}
从方法返回此列表后,您可以使用列表索引访问文本行
我托管了一个文本文件,该文件的组织字符串如下:
- 第一行
- 第二行
- 第三行
- 第四行
- 第六行
- 第七行
- .......
我正在使用以下函数从此文件中获取所有内容:
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;
}
从方法返回此列表后,您可以使用列表索引访问文本行