如何在Metro App中使用StreamReader在c#中编写.txt文件

本文关键字:txt 文件 StreamReader Metro App | 更新日期: 2023-09-27 18:13:29

我在Windows 8 metro应用程序中创建StreamReader对象时遇到了麻烦。我试图让它读取文本文件在我的本地目录,但它一直显示我这个错误。有人对此有什么解决方案或者其他的解决方案吗?

我使用的是MS Visual Studio Ultimate 2012, metro application.

代码:

        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++;
            }
        }

错误显示:

的最佳重载方法'System.IO.StreamReader.StreamReader(System.IO.Stream)'有一些无效的辩论结局。

如何在Metro App中使用StreamReader在c#中编写.txt文件

Windows.Storage.StorageFile。GetFileFromApplicationUriAsync和Windows.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
    }
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++;
        }