0

After I got a file ( example: a.txt) from assests folder, while I'm reading this file line by line

this code part ( line=reader.Readline() ) have to read content of a file's line; but line is getting a file path.

I want to add all line in the file to list.(lines)

****** This project is about Universal App and **Windows Phone 8.1 part

    List<string> lines = new List<string>();

    public async void LoadFile(string file)
    {
        var InstallationFolder = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync("Assets");
        var _file = await InstallationFolder.GetFileAsync(file);
        byte[] byteArray = Encoding.UTF8.GetBytes(file);
        MemoryStream stream = new MemoryStream(byteArray);
        using (var reader = new StreamReader(stream))
        {
            string line = null;
            while ((line = reader.ReadLine()) != null)
            {
                lines.Add(line);
            }
        }  
    }
CinCout
  • 9,486
  • 12
  • 49
  • 67
gkd
  • 11

2 Answers2

0

you don't read the file with your StreamReader. try:

public async void LoadFile(string file)
    {          
        using (var reader = new StreamReader(file))
        {
            string line = null;
            while ((line = reader.ReadLine()) != null)
            {
                lines.Add(line);
            }
        }  
    }

where file is your file path and name.

apomene
  • 14,282
  • 9
  • 46
  • 72
  • var InstallationFolder = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync("Assets"); var _file = await InstallationFolder.GetFileAsync(file); – gkd Jul 30 '15 at 13:06
  • [async without await?](http://stackoverflow.com/questions/21707714/c-sharp-async-without-await) also wondered why you'd want to asynchronously load a file - would you use anything here to justify? [Using Async for File Access](https://msdn.microsoft.com/en-us/library/jj155757.aspx) – jacoblambert Jul 30 '15 at 13:27
  • i look this page but ı dont use FileStream ı dont kmow why my System.IO dont have File and FİleStream – gkd Jul 30 '15 at 13:39
0
var InstallationFolder = await      Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync("Assets");
var _file = await InstallationFolder.GetFileAsync("a.txt");
var content = await Windows.Storage.FileIO.ReadTextAsync(_file);
                        if (content.Contains(word))
                        {
                            lines.Add(word);   
                        }

i have written the code below according to your suggestions.However I got an error message like "An exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.ni.dll but was not handled in user code WinRT information: No mapping for the Unicode character exists in the target multi-byte code page. Additional information: No mapping for the Unicode character exists in the target multi-byte code page. No mapping for the Unicode character exists in the target multi-byte code page. If there is a handler for this exception, the program may be safely continued."

What should I do at this point to overcome this issue?

gkd
  • 11