简体   繁体   English

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

[英]How write a .txt file in c# using StreamReader in Metro App?

I'm having trouble creating a StreamReader object in Windows 8 metro application. 我在Windows 8 Metro应用程序中创建StreamReader对象时遇到问题。 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. 我正在使用MS Visual Studio Ultimate 2012 Metro应用程序。

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. “ System.IO.StreamReader.StreamReader(System.IO.Stream)”的最佳重载方法有一些无效的争论。

Windows.Storage.StorageFile.GetFileFromApplicationUriAsync and Windows.Storage.FileIO.ReadLinesAsync can be used for this. 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
    }
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++;
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM