locked
Unable to read a simple text file Windows Store App RRS feed

  • Question

  • I'm really surprised that the following isn't working. The idea is as follows:

    1. Open a StorageFile async, then wait for that to complete.
    2. Read the file async, then wait for that task to complete.
    3. Return the string that represents the contents of the file.

    I'm getting a valid StorageFile object, but dying on the call to ReadTextAsync. There's no exception, thrown, which isn't a surprise in an async task. I don't know how to modify the code to read the faulted task and figure out what it doesn't like.

    Thanks....

    private async Task<string> GetFile()
    {
        Task<StorageFile> t1 = GetStorageFile();
        Task.WaitAll(t1);
        StorageFile sf = t1.Result as StorageFile;
    
        Task<string> t2 = GetFileText(sf);
        Task.WaitAll(t2);
        string s = (string)t2.Result;
    
        return s;
    }
            
    private async Task<StorageFile> GetStorageFile()
    {
        ApplicationDataContainer localSettings = ApplicationData.Current.LocalSettings;
        StorageFolder tempFolder = ApplicationData.Current.TemporaryFolder;
    
        System.Diagnostics.Debug.WriteLine("About to open lenovo.txt");
    
        StorageFile storageFile = await tempFolder.GetFileAsync("lenovo.txt");
    
        return storageFile;
    }
    
    private async Task<string> GetFileText(StorageFile storageFile)
    {
        string text = string.Empty;
        System.Diagnostics.Debug.WriteLine("About to read contents of lenovo.txt");
    
        try
        {
            text = await Windows.Storage.FileIO.ReadTextAsync(storageFile);
        }
        catch (Exception ex)
        {
            System.Diagnostics.Debug.WriteLine(ex.Message);
        }
    
        System.Diagnostics.Debug.WriteLine(text);
    
    
        return text;
    }


    Randy

    Monday, October 21, 2013 4:59 PM

Answers