[英]I can't read line-by-line from text file. How can I do?
After I got a file ( example: a.txt ) from assests folder, while I'm reading this file line by line 在我从assests文件夹中获取文件(例如: a.txt )之后,我正在逐行读取此文件
this code part ( line=reader.Readline()
) have to read content of a file's line; 该代码部分(
line=reader.Readline()
)必须读取文件行的内容; but line
is getting a file path. 但是
line
正在获取文件路径。
I want to add all line in the file to list.( lines
) 我想补充的所有行的文件列表(
lines
)
****** This project is about Universal App and **Windows Phone 8.1 part ******这个项目是关于Universal App和** Windows Phone 8.1的一部分
List<string> lines = new List<string>();
public async void LoadFile(string file)
{
var InstallationFolder = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync("Assets");
var _file = await InstallationFolder.GetFileAsync(file);
byte[] byteArray = Encoding.UTF8.GetBytes(file);
MemoryStream stream = new MemoryStream(byteArray);
using (var reader = new StreamReader(stream))
{
string line = null;
while ((line = reader.ReadLine()) != null)
{
lines.Add(line);
}
}
}
You can do this: 你可以这样做:
List<string> lines = new List<string>();
public void LoadFile(string file)
{
lines = File.ReadAllLines(file).ToList();
}
But well, File.ReadAllLines(file)
is what you are trying to implement... It returns an array. 但是,您正在尝试实现
File.ReadAllLines(file)
...它返回一个数组。
Simply use this in your code: 只需在您的代码中使用它:
string[] lines = File.ReadAllLines(path);
you don't read the file with your StreamReader
. 您不会使用
StreamReader
读取文件。 try: 尝试:
public async void LoadFile(string file)
{
using (var reader = new StreamReader(file))
{
string line = null;
while ((line = reader.ReadLine()) != null)
{
lines.Add(line);
}
}
}
where file
is your file path and name. file
是您的文件路径和名称。
var InstallationFolder = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync("Assets");
var _file = await InstallationFolder.GetFileAsync("a.txt");
var content = await Windows.Storage.FileIO.ReadTextAsync(_file);
if (content.Contains(word))
{
lines.Add(word);
}
i have written the code below according to your suggestions.However I got an error message like " An exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.ni.dll but was not handled in user code WinRT information: No mapping for the Unicode character exists in the target multi-byte code page. Additional information: No mapping for the Unicode character exists in the target multi-byte code page. No mapping for the Unicode character exists in the target multi-byte code page. If there is a handler for this exception, the program may be safely continued. " 我已根据您的建议编写了以下代码。但是,我在mscorlib.ni.dll中收到诸如“ System.ArgumentOutOfRangeException类型的异常”之类的错误消息,但未在用户代码中处理WinRT信息:没有Unicode映射目标多字节代码页中存在字符。其他信息:目标多字节代码页中不存在Unicode字符的映射。目标多字节代码页中不存在Unicode字符的映射。处理程序,可以安全地继续执行该程序。 ”
What should I do at this point to overcome this issue? 在这一点上我应该怎么做才能克服这个问题?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.