Reading from file in a Windows RT Application [duplicate]

扶醉桌前 提交于 2019-12-08 09:07:10

问题


Possible Duplicate:
How to read a text file line by line Windows RT?

I am trying to read from file line by line in C#.

This is my code

   String filename = "apoel.txt";

   System.IO.StreamReader file = new System.IO.StreamReader(filename);

I followed instructions from an MSDN page and followed them exactly. The problem is I keep getting the errors

The best overloaded method match for System.IO.StreamReader.StreamReader (System.IO.Stream)' has some invalid arguments
Argument 1: cannot convert from 'string' to 'System.IO.Stream'

I added using System.IO; on the top of my code

What am I doing wrong? If it is of any help this is a Windows Metro app

Also can someone explain to me why the article from MSDN that I post is wrong and not working? Do not give me an alternative please. Please tell me why my code is not working while it is explained like that in MSDN


回答1:


Here is the code i use for Reading/Writing a file in Windows 8. It works and i hope it helps you too.

private StorageFolder localFolder;
// Read from a file line by line
public async Task ReadFile()
{
    try
    {
        // get the file
        StorageFile myStorageFile = await localFolder.GetFileAsync("MyDocument.txt");
        var readThis = await FileIO.ReadLinesAsync(myStorageFile);
        foreach (var line in readThis)
        {
            String myStringLine = line;
        }
        Debug.WriteLine("File read successfully.");
    }
    catch(FileNotFoundException ex)
    {   
        Debug.WriteLine(ex);           
    }
}
// Write to a file line by line
public async void SaveFile()
{
    try
    {
        // set storage file
        StorageFile myStorageFile = await localFolder.CreateFileAsync("MyDocument.txt", CreationCollisionOption.ReplaceExisting);
        List<String> myDataLineList = new List<string>();
        await FileIO.WriteLinesAsync(myStorageFile, myDataLineList);
        Debug.WriteLine("File saved successfully.");
    }
    catch(FileNotFoundException ex)
    {  
        Debug.WriteLine(ex);            
    }
}



回答2:


You were reading documentation which didn't take into account the fact that many of the members of StreamReader aren't available in Windows Store apps.

Look at the overall StreamReader documentation. You can only use the members with a green bag next to them.

File access in Windows Store apps is a little different to full desktop .NET. I suggest you read this MSDN guide. Once you've got a Stream, you can build a StreamReader - or you could use the members of Windows.Storage.FileIO such as ReadLinesAsync, depending on what you're trying to do.




回答3:


String[] lines = File.ReadAllLines(filePath);

or

List<string> lines = new List<string>(File.ReadAllLines(filePath));



回答4:


In the example that you post above filename is not initialized to anything. With a later version of the compiler it complains about unassigned use of filename. in any case initialize filename to

string filename = @"c:\somefile.txt";

and it should compile correctly.



来源:https://stackoverflow.com/questions/14164851/reading-from-file-in-a-windows-rt-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!