1

I'm having trouble creating a StreamReader object in Windows 8 metro application. I'm trying to make it read the text file in my local directory but it keep showing me this error. Anyone has any solutions for this or any other alternative solutions?

I'm using MS Visual Studio Ultimate 2012, metro application.

Code:

        int level = 1;

        var fileName = string.Format(@"Mazes\level{0}.txt", level);

        using (var sr = new StreamReader(fileName))
        {
            var l = 0;
            while (!sr.EndOfStream)
            {
                string line = sr.ReadLine();

                for (var c = 0; c < line.Length; c++)
                {
                    mazeValues[c, l] = line[c];

                    if (mazeValues[c, l] == '1')
                    {
                        var glass = new Glass();
                        glass.SetValue(Grid.ColumnProperty, c);
                        glass.SetValue(Grid.RowProperty, l);
                        grdMaze.Children.Add(glass);
                        mazeGlasses[c, l] = glass;
                    }
                }
                l++;
            }
        }

Error showing:

The best overloaded method for 'System.IO.StreamReader.StreamReader(System.IO.Stream)' has some invalid arguements.

4

2 回答 2

1

Windows.Storage.StorageFile.GetFileFromApplicationUriAsyncWindows.Storage.FileIO.ReadLinesAsync可用于此。

using System;
using Windows.Storage;
async void test2()
{

    var fileName = string.Format(@"ms-appx:///Mazes/level{0}.txt", level);
    try 
    {
        var file = await StorageFile.GetFileFromApplicationUriAsync(new Uri( "ms-appx:///assets/textfile1.txt"));
        var lines = await FileIO.ReadLinesAsync(file);
        foreach (var line in lines)
        {
            // code to process each line here
        }
    }
    catch (Exception e)
    {
         // handle exceptions
    }
于 2013-05-14T04:26:43.890 回答
0
WebClient client = new WebClient();       
System.IO.Stream stream = client.OpenRead(path_of_text _file);
     System.IO.StreamReader str = new StreamReader(stream);
     string Text = str.ReadLine();

   while (!Text.EndOfStream)
        {
            string line = Text.ReadLine();

            for (var c = 0; c < line.Length; c++)
            {
                mazeValues[c, l] = line[c];

                if (mazeValues[c, l] == '1')
                {
                    var glass = new Glass();
                    glass.SetValue(Grid.ColumnProperty, c);
                    glass.SetValue(Grid.RowProperty, l);
                    grdMaze.Children.Add(glass);
                    mazeGlasses[c, l] = glass;
                }
            }
            l++;
        }
于 2013-05-14T05:58:23.320 回答