Search code examples
c#windows-phone-8.1windows-8.1storagefile

GetFileAsync does not return file after saving it


I have the following method, which saves a file to my LocalFolder on the Windows Phone

public async Task<bool> SaveValueByKey(string filename, KeyValuePair<string, string> dataToSave)
{
    var appData = ApplicationData.Current;

    // Good Save documentation - http://chriskoenig.net/2012/09/07/windows-8-games-and-roaming-data/
    StorageFile file = await appData.LocalFolder.CreateFileAsync(filename, CreationCollisionOption.OpenIfExists);

    try
    {
        // Delete the key if it already exists, then replace.
        IEnumerable<string> content = await this.LoadFileContents(filename);
        if (content.Any(l => l.StartsWith(string.Format("{0}==", dataToSave.Key))))
        {
            await this.DeleteKey(filename, dataToSave.Key);
        }

        await FileIO.WriteTextAsync(file, string.Format("{0}=={1}", dataToSave.Key, dataToSave.Value));
    }
    catch (Exception)
    {
        return false;
    }

    return true;
}

I then later try to load the data back using this method.

public async Task<string> LoadValueFromKey(string filename, string key)
{
    var appData = ApplicationData.Current;
    StorageFile file = null;

    try
    {
        file = await appData.LocalFolder.GetFileAsync(filename);
    }
    catch (FileNotFoundException)
    {
        return string.Empty;
    }
    catch (Exception)
    {
        return string.Empty;
    }

    IEnumerable<string> contents = await this.LoadFileContents(filename);
    string element = string.Format("{0}==", key);
    if (!contents.Any(line => line.StartsWith(element)))
    {
        return string.Empty;
    }

    return contents.First(line => line.StartsWith(element))
        .ToString()
        .Substring(element.Length);
}

The call to GetFileAsync returns null, even though a valid file object was returned to me after the CreateFileAsync call.

Am I missing something here? Are there declarations I need to make? I didn't see any that were related to this.

Edit

The filename and key values are passed in from a Repository, which declares them like this:

private const string tokenFilename = "UserSession.dat";

private const string tokenKey = "UserToken";

Edit 2

The exception being thrown is a FileNotFound exception with the following message.

The system cannot find the file specified. (Exception from HRESULT: 0x80070002)

at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at Lifestream.App.Mobile.WindowsApp.Services.FileStorageService.<LoadValueFromKey>d__16.MoveNext()

Edit 3

If I run the app as a Windows app (this is a Universal app), the file is saved and loaded without any issues. If I target the Windows Phone then the File Not Found exception is thrown when I try to save and then load. This seems to be specific to the phone and I can't figure why


Solution

  • I was able to solve this. The issue was with the Windows Phone project properties. Properties->Debug had the "Uninstall then reinstall my package" option checked, which was deleting all of the saved files I was creating each time I re-ran the application.