UWP: Local app data

Source: Internet
Author: User

Get settings and file containers for your app

Use the applicationdata.localsettings property to get the settings in the Applicationdatacontainer object. Use the Applicationdata.localfolder property to get the files in the Storagefolder object.

Here is a helper class for the corresponding data manipulation in my package, mainly around the above two APIs:

    Public classAppdatahelper {#regionField/// <summary>        ///get the app's settings container/// </summary>        Private StaticWindows.Storage.ApplicationDataContainer localsettings =Windows.Storage.ApplicationData.Current.LocalSettings; /// <summary>        ///get isolated Storage files/// </summary>        Private StaticWindows.Storage.StorageFolder Localfolder =Windows.Storage.ApplicationData.Current.LocalFolder; #endregion        #regionSet application settings (simple settings, composite settings, settings in the container)/// <summary>        ///Simple Setup/// </summary>        /// <param name= "key" ></param>        /// <param name= "value" ></param>         Public Static voidSetValue (stringKeystringvalue) {Localsettings.values[key]=value; }        /// <summary>        ///Composite Settings/// </summary>        /// <param name= "composite" ></param>         Public Static voidSetcompositevalue (Windows.Storage.ApplicationDataCompositeValue composite) {composite["intval"] =1; composite["Strval"] ="string"; localsettings.values["examplecompositesetting"] =Composite; }        /// <summary>        ///Create a settings container/// </summary>        /// <param name= "ContainerName" ></param>        /// <returns></returns>        Private StaticWindows.Storage.ApplicationDataContainer CreateContainer (stringcontainername) {            returnLocalsettings.createcontainer (ContainerName, Windows.Storage.ApplicationDataCreateDisposition.Always); }        /// <summary>        ///Speaking settings save to Settings container/// </summary>        /// <param name= "ContainerName" ></param>        /// <param name= "key" ></param>        /// <param name= "value" ></param>         Public Static voidSetcontainervalue (stringContainerName,stringKeystringvalue) {            if(!LocalSettings.Containers.ContainsKey (containername))            CreateContainer (containername); Localsettings.containers[containername]. Values[key]=value; }        #endregion        #regionGet app settings (simple settings, composite settings, settings in the container)/// <summary>        ///Get app Settings/// </summary>        /// <param name= "key" ></param>        /// <returns></returns>         Public Static ObjectGetValue (stringkey) {            returnLocalsettings.values[key]; }        /// <summary>        ///Get composite Settings/// </summary>        /// <param name= "Compositekey" ></param>        /// <returns></returns>         Public StaticWindows.Storage.ApplicationDataCompositeValue Getcompositevalue (stringCompositekey) {            //Composite SettingWindows.Storage.ApplicationDataCompositeValue composite =(Windows.Storage.ApplicationDataCompositeValue) Localsettings.values[compositekey]; returnComposite; }        /// <summary>        ///get app settings from the settings container/// </summary>        /// <returns></returns>         Public Static ObjectGetvaluebycontainer (stringContainerName,stringkey) {            BOOLHascontainer =LocalSettings.Containers.ContainsKey (containername); if(hascontainer) {returnLocalsettings.containers[containername].            Values.containskey (key); }            return NULL; }        #endregion        #regionRemove the settings that have been completed/// <summary>        ///remove simple settings or composite settings/// </summary>        /// <param name= "key" ></param>         Public Static voidRemove (stringkey)        {LocalSettings.Values.Remove (key); }        /// <summary>        ///Delete Settings Container/// </summary>        /// <param name= "key" ></param>         Public Static voidRemovecontainer (stringcontainername)        {Localsettings.deletecontainer (containername); }        #endregion        #regionFile Store Operations/// <summary>        ///Write File/// </summary>         Public Async voidWritetimestamp (stringFileName,stringcontents) {            Try{Windows.Storage.StorageFile Samplefile=awaitLocalfolder.createfileasync (FileName, Windows.Storage.CreationCollisionOption.ReplaceExisting); awaitWindows.Storage.FileIO.WriteTextAsync (samplefile, contents); }            Catch(Exception) {Throw; }                  }        /// <summary>        ///Read File/// </summary>         Public Asynctask<string> Readtimestamp (stringfileName) {            Try{Windows.Storage.StorageFile Samplefile=awaitLocalfolder.getfileasync (fileName); stringContents =awaitWindows.Storage.FileIO.ReadTextAsync (Samplefile); returncontents; }            Catch(Exception) {return "Read Faild"; }        }        #endregion    }

UWP: Local app data

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.