Windows8 ApplicationData

        ApplicationDataContainer roamingSettings = ApplicationData.Current.RoamingSettings;
        StorageFolder roamingFolder = Windows.Storage.ApplicationData.Current.RoamingFolder;

        public MainPage()
        {
            this.InitializeComponent();

            //Set Simple setting
            roamingSettings.Values["exampleSetting"] = "Hello World";
            roamingSettings.Values["HighPriority"] = "65";

            //Set Composite setting
            ApplicationDataCompositeValue composite = new ApplicationDataCompositeValue();
            composite["intVal"] = 1;
            composite["strVal"] = "string";
            roamingSettings.Values["exampleCompositeSetting"] = composite;

            //Set ApplicationContainer
            ApplicationDataContainer container = roamingSettings.CreateContainer("exampleContainer", ApplicationDataCreateDisposition.Always);
            if (roamingSettings.Containers.ContainsKey("exampleContainer"))
                roamingSettings.Containers["exampleContainer"].Values["exampleSetting"] = "Hello World";

            //Get Simple setting
            Object value = roamingSettings.Values["exampleSetting"];

            //Get Composite setting
            ApplicationDataCompositeValue compositeValue =(ApplicationDataCompositeValue)roamingSettings.Values["exampleCompositeSetting"];
            if (compositeValue != null)
            {
                var item1 = composite["intVal"];
                var item2 = composite["strVal"];
            }

            // Setting in a container
            bool hasContainer = roamingSettings.Containers.ContainsKey("exampleContainer");
            bool hasSetting = false;
            if (hasContainer)
            {
                hasSetting = roamingSettings.Containers["exampleContainer"].Values.ContainsKey("exampleSetting");
                var item = roamingSettings.Containers["exampleContainer"].Values["exampleSetting"];
            }

            // Delete simple setting
            roamingSettings.Values.Remove("exampleSetting");

            // Delete composite setting
            roamingSettings.Values.Remove("exampleCompositeSetting");

            // Delete container
            roamingSettings.DeleteContainer("exampleContainer");
        }


        async void WriteTimes()
        {
            StorageFile sampleFile = await roamingFolder.CreateFileAsync("msiApplicationData.txt",CreationCollisionOption.OpenIfExists);
            await FileIO.WriteTextAsync(sampleFile,"Hello,Jack");
        }

        async void ReadTimes()
        {
            try
            {
                StorageFile sampleFile = await roamingFolder.GetFileAsync("msiApplicationData.txt");
                String content = await FileIO.ReadTextAsync(sampleFile);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
        }

 

posted on 2013-03-28 09:51  JackSlaterYu  阅读(221)  评论(0编辑  收藏  举报