Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用StreamReader逐行读取文件_C#_Windows 8_Windows Runtime_Streamreader - Fatal编程技术网

C# 使用StreamReader逐行读取文件

C# 使用StreamReader逐行读取文件,c#,windows-8,windows-runtime,streamreader,C#,Windows 8,Windows Runtime,Streamreader,我正在尝试使用StreamReader在Windows8C#文件中逐行读取一个简单的文本文件。我尝试了许多解决方案,但没有一个奏效。有时读取文件,有时不读取(类为空,仅为null)。如果程序正在运行,它永远不会工作,但当我设置一个调试器并使用它逐行运行时,有时它会工作。为什么呢?这就是来源: public async void read(string fileName) { string File = Path.Combine(Package.Current.Insta

我正在尝试使用StreamReader在Windows8C#文件中逐行读取一个简单的文本文件。我尝试了许多解决方案,但没有一个奏效。有时读取文件,有时不读取(类为空,仅为null)。如果程序正在运行,它永远不会工作,但当我设置一个调试器并使用它逐行运行时,有时它会工作。为什么呢?这就是来源:

public async void read(string fileName)
    {

        string File = Path.Combine(Package.Current.InstalledLocation.Path, "myfolder", fileName);

        StorageFolder folder = await StorageFolder.GetFolderFromPathAsync(Path.GetDirectoryName(File));

        Stream s = await folder.OpenStreamForReadAsync(Path.GetFileName(File));
        StreamReader streamReader = new StreamReader(s);

        myClass.name = streamReader.ReadLine();
        myClass.desc = streamReader.ReadLine();
        myClass.number = int.Parse(streamReader.ReadLine());
        myClass.house = new House[myClass.number];

        for (int i = 0; i < myClass.number; i++)
        {
            myClass.house[i] = new House();
            myClass.house[i].name = streamReader.ReadLine();
            myClass.house[i].size = double.Parse(streamReader.ReadLine());
        }  
    }
公共异步无效读取(字符串文件名)
{
string File=Path.Combine(Package.Current.InstalledLocation.Path,“myfolder”,文件名);
StorageFolder folder=等待StorageFolder.GetFolderFromPathAsync(Path.GetDirectoryName(文件));
Stream s=wait folder.OpenStreamForReadAsync(Path.GetFileName(File));
StreamReader StreamReader=新的StreamReader;
myClass.name=streamReader.ReadLine();
myClass.desc=streamReader.ReadLine();
myClass.number=int.Parse(streamReader.ReadLine());
myClass.house=新房子[myClass.number];
对于(int i=0;i
如果函数是事件处理程序(例如,按下按钮),则只应使用
async void
。通常,您应该使用
异步任务

将方法签名从
void
更改为
Task

public async Task read(string fileName)
然后从您的呼叫代码中,
等待它:

await yourclass.read("c:\\my_filename.txt");

我试过了,但我想在初始化整个页面时调用该方法,但当我在页面构造函数中编写“await read”(“filetoRead.txt”)”时,它表示“await”运算符只能在异步方法中使用,但据我所知,构造函数不能是异步的。我是对的还是遗漏了什么?@user2202439-关于异步构造函数:看看这个谢谢你的帮助。我读了斯蒂芬的博客好几次,但还是写不好。我有私有的
异步任务InitializeAsync()
,其中我只使用StreamReader读取数据并重新运行此任务;还有
公共静态异步任务CreateAsync()
,其中我使用wait调用InitializeAsync。我通过
公共静态异步任务UseMyClassAsync(){instance=wait MyClass.CreateAsync();}
在代码中调用这些方法,但它仍然存在问题。我做错了什么?你能给我一个例子看看它应该是什么样子吗?@user2202439如果你试图从构造函数调用
wait MyClass.CreateAsync()
,它就是做不到。你必须找到其他方法来调用它。例如,如果调用者是UI页面,则可以从
LoadState
OnNavigateTo
调用
MyClass.CreateAsync
。这假设您的项目是使用其中一个标准模板(例如网格或拆分应用程序模板)创建的。除此之外,我不能写一个例子,因为我只会猜测你想做什么。你最好提出一个新问题,并提供更多的背景。你完全正确。我完全忘记了导航到
时的
。我将它重写为异步方法,现在它工作得很好。再次感谢您的帮助和时间。