Reading from file in a Windows RT Application [dup

2019-06-13 20:44发布

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

4条回答
仙女界的扛把子
2楼-- · 2019-06-13 20:57

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.

查看更多
霸刀☆藐视天下
3楼-- · 2019-06-13 21:02

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.

查看更多
Melony?
4楼-- · 2019-06-13 21:03

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);            
    }
}
查看更多
beautiful°
5楼-- · 2019-06-13 21:13
String[] lines = File.ReadAllLines(filePath);

or

List<string> lines = new List<string>(File.ReadAllLines(filePath));
查看更多
登录 后发表回答