无法读取 Metro 应用程序中的文本文件? [英] Can't read the text file in metro apps?

查看:15
本文介绍了无法读取 Metro 应用程序中的文本文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我第一次可以读取文本文件.当我下次尝试读取同一个文本文件时,它退出函数并返回空值.

I can read the text file for first time. when i try to read the same text file next time, it quit the function and return null value.

    static string configData = "";
    async public void readtextFile(string folder, string file)
    {
        StorageFolder storageFolder = await Package.Current.InstalledLocation.GetFolderAsync(folder);
        StorageFile storageFile = await storageFolder.GetFileAsync(file);
        configData = await FileIO.ReadTextAsync(storageFile);
    }

请给我建议,如何解决这个问题..

Please suggest me, how to resolve this issue..

谢谢谢赫阿卜杜拉

推荐答案

不要忘记 readtextFile 是一种异步方法.当你调用它时,它实际上在到达第一个 await 时返回,所以此时 configData 尚未设置.您应该从方法返回值,并等待方法:

Don't forget that readtextFile is an asynchronous method. When you call it, it actually returns when it reaches the first await, so at this point configData is not set yet. You should return the value from the method, and await the method:

async public Task<string> readtextFile(string folder, string file)
{
    StorageFolder storageFolder = await Package.Current.InstalledLocation.GetFolderAsync(folder);
    StorageFile storageFile = await storageFolder.GetFileAsync(file);
    string configData = await FileIO.ReadTextAsync(storageFile);
    return configData;
}

...

string configData = await readTextFile(folder, file);

即使你想在一个字段中存储configData,你仍然需要在读取值之前await readtextFile.

Even if you want to store configData in a field, you still need to await readtextFile before you read the value.

这篇关于无法读取 Metro 应用程序中的文本文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆