Can't read the text file in metro apps?

跟風遠走 提交于 2019-12-13 00:27:24

问题


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..

Thanks SheikAbdullah


回答1:


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);

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



来源:https://stackoverflow.com/questions/10512355/cant-read-the-text-file-in-metro-apps

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!