我在Windows 8 metro应用程序中创建StreamReader对象时遇到问题。我正在尝试让它读取我本地目录中的文本文件,但它一直向我显示此错误。任何人都有这个或任何其他替代解决方案的解决方案吗?
我正在使用MS Visual Studio Ultimate 2012,地铁应用程序。
代码:
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)'有一些 无效的争论。
答案 0 :(得分:1)
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
}
答案 1 :(得分: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++;
}