Roaming Options for User Settings: Listing 3

Retrieving cloud settings.

public static void RetrieveCloudSettings()
{
    LiveClient.GetAsync("/me/skydrive/my_documents/files/",false);
}

private static void FindCloudSettingsFile(IDictionary<string, object> result)
{
    var data = (List<object>)result["data"];

    foreach (IDictionary<string, object> album in data)
    {
        var filename = album.SafeValue<string>("name");
        if (filename == CloudSettingsFile)
        {
            var source = album.SafeValue<string>("source");
            LiveClient.DownloadAsync(source);
            return;
        }
    }
}

private static void FileDownloadCompleted(object sender, LiveDownloadCompletedEventArgs e)
{
    var tmpFile = Guid.NewGuid().ToString();
    try
    {
        using (var file = IsolatedStorageFile.GetUserStoreForApplication().OpenFile(tmpFile,
                                                                                    FileMode.Create,
                                                                                    FileAccess.Write))
        {
            e.Result.CopyTo(file);
        }

        ReloadSettings(tmpFile);

        if (File.Exists(tmpFile))
        {
            File.Delete(tmpFile);
        }
    }
    catch(Exception ex)
    {
                
    }
}

private static void ReloadSettings(string temporarySettingsFile)
{
    var settings = new Dictionary<string, object>();
    using (var file = IsolatedStorageFile.GetUserStoreForApplication().OpenFile(temporarySettingsFile, FileMode.Open,
      FileAccess.Read))
    {
        using (var streamReader = new StreamReader(file))
        {
            if (file.Length > 0)
            {
                try
                {
                    var list = new List<Type>();
                    var text = streamReader.ReadLine()+"";
                    var separator = new char[1];
                    var array = text.Split(separator);
                    for (int i = 0; i < array.Length; i++)
                    {
                        var typeName = array[i];
                        Type type = Type.GetType(typeName, false);
                        if (type != null)
                        {
                            list.Add(type);
                        }
                    }
                    file.Position = text.Length + Environment.NewLine.Length;
                    var dataContractSerializer = new DataContractSerializer(typeof(Dictionary<string, object>), list);
                    settings = (Dictionary<string, object>)dataContractSerializer.ReadObject(file);
                }
                catch(Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                    return;
                }
            }
        }
    }
    IsolatedStorageSettings.ApplicationSettings.Clear();
    foreach (var setting in settings)
    {
        IsolatedStorageSettings.ApplicationSettings[setting.Key] = setting.Value;
    }
    IsolatedStorageSettings.ApplicationSettings.Save();

    SettingsRetrievedFromCloud(LiveClient, EventArgs.Empty);
}

About the Author

Nick Randolph runs Built to Roam, a consulting company that specializes in training, mentoring and assisting other companies build mobile applications. With a heritage in rich client applications for both the desktop and a variety of mobile platforms, Nick currently presents, writes and educates on the Windows Phone platform.

comments powered by Disqus

Featured

  • Analyst: TypeScript Now Firmly in Top 10 Echelon (Ruby, Not So Much)

    RedMonk analyst Stephen O'Grady believes TypeScript has achieved the rare feat of firmly ensconcing itself into the top 10 echelon of his ranking, now questioning how high it might go.

  • Black White Wave IMage

    Neural Regression Using PyTorch: Training

    The goal of a regression problem is to predict a single numeric value, for example, predicting the annual revenue of a new restaurant based on variables such as menu prices, number of tables, location and so on.

  • Microsoft Ships Visual Studio 2019 v16.9 Servicing Baseline Release

    Microsoft is urging enterprises and professional coders to standardize on the new Visual Studio 2019 v16.9, a servicing baseline release that's guaranteed to receive official support for an extended period.

  • Microsoft Extends Low-Code Push with Power Fx Programming Language

    "Using formulas that are already familiar to hundreds of millions of users, Power Fx allows a broad range of people to bring skills they already know to low code solutions."

Upcoming Events