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

  • Kubernetes for Developers

    Microsoft's Dan Wahlin previews his introductory "Kubernetes for Developers" session at Visual Studio Live! San Diego 2026, explaining how developers can get past the Kubernetes learning curve by starting locally, mastering Pods first, and using Services to make containerized applications reliably accessible.

  • VS Code Keeps Eye on Costs in v1.126 Update

    Visual Studio Code 1.126 adds session-level Copilot cost information, continuing Microsoft's recent focus on helping developers monitor and manage usage-based GitHub Copilot billing.

  • Open VSX 1.0.0 Puts Focus on Open Extension Registry for VS Code Ecosystem

    Eclipse Open VSX has reached 1.0.0, highlighting its role as a vendor-neutral registry for VS Code-compatible extensions.

  • Infragistics Puts MCP Toolchain at Center of Ultimate 26.1

    Infragistics Ultimate 26.1 introduces the Ignite UI Enterprise MCP toolchain for AI-assisted app development across Angular, React, Web Components and Blazor.

Subscribe on YouTube