andreas
andreas

Reputation: 8004

Store data in Windows Store app

Short version: How would you store "large" data in a Windows Store app, using C#?

Long version: I have a json string that I could either store as string (around 65 KB) or - when serialized - as object. I originally thought that I could use something like this:

    public static void SaveData(string param, object value)
    {
        Windows.Storage.ApplicationDataContainer settings = Windows.Storage.ApplicationData.Current.LocalSettings;       
        Windows.Storage.ApplicationDataCompositeValue composite = new Windows.Storage.ApplicationDataCompositeValue();
        composite[param] = value;
        settings.Values[param] = composite;
    }

I tried that, but my app shows the following error:

WinRT information: Error trying to write setting in application data composite value

The error is not really helping me, it simply does not want to save my data. I have just read online that there is only a ridiculous small amount of Bytes allowed to store in the local settings in a Windows Store app using C#.

Is there a fast, safe and elegant way to store my string/object otherwise? Do I really need to write it into a file by myself? Using SQLLite would also be way too much for just this string.

Upvotes: 6

Views: 6694

Answers (4)

narendra yadav
narendra yadav

Reputation: 1

Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;

string _value="You value";

localSettings.Values["Your Variable name"]=_value;

Upvotes: -2

Rahul Ranjan
Rahul Ranjan

Reputation: 274

For your help, I am even giving the code here :- Use LocalFolder instead of LocalSettings as given below.

public static class AppSystem
{
    #region FileExistsMethod
    public static async Task<bool> FileExists(string filename)
    {
        StorageFolder folder = ApplicationData.Current.LocalFolder;
        IReadOnlyList<StorageFile> list_of_files = await folder.GetFilesAsync();

        foreach(StorageFile file in list_of_files)
        {
            if (file.DisplayName == "Data")
                return true;
        }
        return false;
    }
    #endregion
    public static async Task<ObservableCollection<Notes>> GetCollection()
    {
        ObservableCollection<Notes> temp = new ObservableCollection<Notes>();

        StorageFolder folder = ApplicationData.Current.LocalFolder;

        if (await AppSystem.FileExists("Data"))
        {
            StorageFile file = await folder.GetFileAsync("Data.txt");
            using (Stream stream = await file.OpenStreamForReadAsync())
            {
                DataContractJsonSerializer json = new DataContractJsonSerializer(typeof(Notes));
                temp = (ObservableCollection<Notes>)json.ReadObject(stream);
            }
        }   
        return temp;
    }

    public static async void Save(Notes notes)
    {
        ObservableCollection<Notes> temp = new ObservableCollection<Notes>();
        StorageFolder folder = ApplicationData.Current.LocalFolder;
        bool exists = await AppSystem.FileExists("Data");

        if(exists==true)
        {
            StorageFile file = await folder.GetFileAsync("Data");
            using(Stream stream = await file.OpenStreamForReadAsync())
            {
                DataContractJsonSerializer json = new DataContractJsonSerializer(typeof(Notes));
                temp = (ObservableCollection<Notes>)json.ReadObject(stream);
            }
            await file.DeleteAsync(StorageDeleteOption.PermanentDelete);
        }

        temp.Add(notes);

        StorageFile file1 = await folder.CreateFileAsync("Data", CreationCollisionOption.ReplaceExisting);

        using(Stream stream = await file1.OpenStreamForWriteAsync())
        {
            DataContractJsonSerializer json = new DataContractJsonSerializer(typeof(Notes));
            json.WriteObject(stream, temp);
        }
    }
}

Whenever I wish to use saving and loading of data, I prefer to make a static class and put all the methods there and then use it the app at places where it is required. Here, Notes is the class and i m storing the list of Notes object by serializing it into json string

Upvotes: 1

yasouser
yasouser

Reputation: 5177

The data you are trying to store in local settings container exceeds the limits. Quoting from here: ApplicationData.LocalSettings | localSettings property

The name of each setting can be 255 characters in length at most. Each setting can be up to 8K bytes in size and each composite setting can be up to 64K bytes in size.

You can use ApplicationData.LocalFolder | localFolder instead to store the data. There is sample code is in that link too.

Upvotes: 10

ChrisLively
ChrisLively

Reputation: 88064

Considering the purpose of the ApplicationDataContainer is simply for app settings, then the size limit makes perfect sense. That limit is 8kb per setting; which is MORE than enough for application settings.

Storing larger amounts of data in a file or db is not just the only way, it's the correct way.

Upvotes: 3

Related Questions