簡體   English   中英

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

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

我在Windows 8 Metro應用程序中創建StreamReader對象時遇到問題。 我正在嘗試使其讀取本地目錄中的文本文件,但它一直向我顯示此錯誤。 有人對此有任何解決方案或任何其他替代解決方案嗎?

我正在使用MS Visual Studio Ultimate 2012 Metro應用程序。

碼:

        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)”的最佳重載方法有一些無效的爭論。

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