-1

I am using the following code to read a text file from the Assets Folder into a string and the split it into array

string filepath = @"Assets\DATA.csv";
StorageFolder folder =     Windows.ApplicationModel.Package.Current.InstalledLocation;
StorageFile file = await folder.GetFileAsync(filepath); // error here
var Lines = await Windows.Storage.FileIO.ReadTextAsync(file);
string[] lines2 = Lines.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);

This works fine but the textfile is now over 500 lines and I am getting an exception as its too big to fit in the string

Is there a way of reading the text file directly into an array a line at a time Each line is terminated by a newline

I have searched and there seem to be a way of doing it using file. class but I can get it to work.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Mark Barr
  • 159
  • 2
  • 12
  • 1
    "I have searched and there seem to be a way of doing it using file. class but I can get it too work" - show us what u tried – pm100 Feb 03 '15 at 23:46
  • https://msdn.microsoft.com/en-us/library/94223t4d.aspx – pm100 Feb 03 '15 at 23:46
  • use StreamReader class – ako Feb 03 '15 at 23:48
  • I can do the same thing never had that error .. also how come you are splitting on `Environment.NewLine` instead of `,` also have you used the `Debugger` – MethodMan Feb 03 '15 at 23:48
  • 1
    ako is right - use the StreamReader as it already abstracts the end of line stuff for you (see the answer from chouaib). @MethodMan when looping over a CSV it makes sense to get all rows and then split the rows into columns...unless using a two dimensional array to store the values – Robert Petz Feb 03 '15 at 23:59
  • @RobertPetz I've never had issue parsing a .csv file and unless he can show an example of the .`csv` file then I think that you are a bit off track. – MethodMan Feb 04 '15 at 00:01
  • @MethodMan - I just edited my response - I didn't realize he was reading in a CSV file so I had misunderstood why you were asking about him splitting on a comma - apologies =) – Robert Petz Feb 04 '15 at 00:02
  • his first line of code `string filepath = @"Assets\DATA.csv";` was the dead give away... lol – MethodMan Feb 04 '15 at 00:05
  • He wants to read a file line by line right? **Don't use StreamReader, don't reinvent the wheel**. Most of the time in .NET System.IO.File is better and in Windows RT you have the Readasync methods. While we're discussing DRTW have you tried [CsvHelper](https://github.com/JoshClose/CsvHelper)? Btw, I assume this is a .NET Core project (like a win 8 store app), hence no IO? – Nathan Cooper Feb 04 '15 at 00:10
  • All the searches I tried suggested i use string[] readText = File.ReadAllLines(path); to read into an array but it keeps saying file doesnt exists in this context im pretty new at this and dont know what I have done to deserve a downvote – Mark Barr Feb 04 '15 at 08:38
  • Yes pleas I want to read a file line by line but my code says system.io doesnt have a reference for file – Mark Barr Feb 04 '15 at 08:45
  • Everytime i use streamreader and console as in the apps I get many errors such as streamreader has invald arguments and console does not exists in system are you missing a referene as suggested in the links provided – Mark Barr Feb 04 '15 at 09:00
  • Although I call the file DATA.csv its just a text file with 5 items seprated by a comma and ending in an newline It currently has 500 of these lines but will eventuall have 3000+ when the file had only 250 lines it worked fine with my existing code – Mark Barr Feb 04 '15 at 09:01
  • The project I am using is a win store c# app – Mark Barr Feb 04 '15 at 09:03
  • Guys SOLVED IT!!! After searching for the full eror itself I found someone who suggested changing the encoding on the file To UTF -8 with signature I did this and it runs with all 500 lines Not sure why but it works thank you all so much for your help I really want to leanr to use streamwriter and other methods as I am sure there is a better way of doing this but until my knowledge gets better I will persevere Thank You – Mark Barr Feb 04 '15 at 09:22
  • I'm glad to see the OP has solved their problem. I doubt they used System.IO.File in a store app, however (btw can be done with unsafe code dark magic). So I think the OP's understanding of the problem still has rooom for improvement, try following links in my answer. Given that the accepted here uses System.IO.File and there's a lot of wanton StreamReader use, if you've come here for reference, go to [Reading from file in a Windows RT Application](http://stackoverflow.com/questions/14164851/reading-from-file-in-a-windows-rt-application/14164890#14164890). – Nathan Cooper Feb 04 '15 at 10:10

3 Answers3

0

you can read it to a list of strings, each line of the text in one element of the list

List<string> myList= new List<string>();
StreamReader sr = new StreamReader(filepath);

while(!sr.EndOfStream)
{
  myList.Add(sr.ReadLine());
}
chouaib
  • 2,763
  • 5
  • 20
  • 35
  • Tried this but says list could not be found are you missing a reference does it need a different refernce other than system and IO? – Mark Barr Feb 04 '15 at 08:44
  • @MarkBarr, `L` in the key word `List` should be in upperCase, I edited the answer, can you try it again – chouaib Feb 04 '15 at 08:50
  • it still has one error new streamreader (filepath) is underlined saying The best overloaded method match for 'System.IO.StreamReader.StreamReader(System.IO.Stream)' has some invalid arguments – Mark Barr Feb 04 '15 at 14:07
  • I wonder what's happening there! but if you use it as `new streamreader(filepath)` yes that's wrong syntax it should be `new StreamReader(filepath);`ss – chouaib Feb 04 '15 at 23:55
0

You want Windows.Storage.FileIO.ReadLinesAsync which returns a list of the lines of the file.

List<string> myLines = ReadLinesAsync(myStorageFile);
Nathan Cooper
  • 6,262
  • 4
  • 36
  • 75
  • Tried this with he following List myLines = ReadLinesAsync(file); but says it doesnt exist in the current context your help greatly appreciated – Mark Barr Feb 04 '15 at 08:40
0

Use

System.IO.File.ReadLines(filepath)

https://msdn.microsoft.com/en-us/library/vstudio/system.io.file.readlines(v=vs.100).aspx

Daniel James Bryars
  • 4,429
  • 3
  • 39
  • 57