簡體   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