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
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
and it should compile correctly.
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 aStreamReader
- or you could use the members ofWindows.Storage.FileIO
such asReadLinesAsync
, depending on what you're trying to do.Here is the code i use for Reading/Writing a file in Windows 8. It works and i hope it helps you too.
or