繁体   English   中英

在Windows 8.1应用程序中一次读取一行文本文件

[英]Reading a Text File One Line at a Time in Windows 8.1 Application

我将此代码添加到OnNavigatedTo函数中:

int counter = 0;   
string line;
string path = "Streams\\Usernames.txt";

// Read the file and display it line by line.
System.IO.StreamReader file =
   new System.IO.StreamReader(path);
while ((line = file.ReadLine()) != null)
{
    Lbx_Usernames.Items.Add(line);
    counter++;
}

但我不断收到此错误:

无法从“字符串”转换为“ System.IO.Steam”

首先,在桌面应用程序和Windows 8.1(通用)应用程序中使用的API集有很多差异。

这个

System.IO.StreamReader file = new System.IO.StreamReader(string path);

仅在桌面应用程序 (控制台,WinForm / WPF)中可用。

Windows Store应用程序中 ,可以使用FileIO.ReadLinesAsync(IStorageFile)从文件中读取所有文本,该文件接受一个StorageFile作为参数,并返回行列表,并且可以循环浏览每行。

示例可以帮助您入门。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM