简体   繁体   中英

How do I save a specific number of text files in Isolated Storage..?

In my WP7 app the users are able send mesage to some server connected.

For this I have a textbox named "msgBox".

And a button "sendBtn" which sends the text written in msgBox to the server.

I want to save the last 20 send messages, not more than that.

Like when the 21st msg will be delivered, the 1st message gets deleted, 2nd one becomes 1st......

and the new msg becomes 20th saved message.

I am not very much familiar with Isolated storage, very basic knowledge.

I cannot figure out how to do this.

I will be very thankful if somebody can help me.

The behavior what you have described fits Queue . But the problem with .Net Queue is that it cannot be saved directly to Isolated Storage (serialization issue). Since you have only 20 elements is good enough to use a list and just remove the first element.

 private List<string> messages = new List<string>(MAX_ITEMS);
 private const int MAX_ITEMS = 20;
 private void userAddMessege(string message)
 {

        messages.Add(message);
        if (messages.Count > MAX_ITEMS) 
        {
            messages.RemoveAt(0);
        }
  }

I assume you want to use IsolatedStorage, because you want to save the messages when the user quits your app and restore the messages the user comes back. I use a simple wrapper

using System;
using System.Net;
using System.Windows;
using System.IO.IsolatedStorage;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace YourProjectNamespace
{
    public class AppSettings
    {
        // Isolated storage settings
        private IsolatedStorageSettings m_isolatedStore;

        public AppSettings()
        {
            try
            {
                // Get the settings for this application.
                m_isolatedStore = IsolatedStorageSettings.ApplicationSettings;
            }
            catch (Exception e)
            {
                MessageBox.Show("Exception while using IsolatedStorageSettings: " + e.ToString());
            }
        }

        /// <summary>
        /// Update a setting value for our application. If the setting does not
        /// exist, then add the setting.
        /// </summary>
        /// <param name="Key">Key to object</param>
        /// <param name="value">Object to add</param>
        /// <returns>if value has been changed returns true</returns>
        public bool AddOrUpdateValue(string Key, Object value)
        {
            bool valueChanged = false;

            try
            {
                if (m_isolatedStore.Contains(Key))
                {
                    // if new value is different, set the new value.
                    if (m_isolatedStore[Key] != value)
                    {
                        m_isolatedStore[Key] = value;
                        valueChanged = true;
                    }
                }
                else
                {
                    m_isolatedStore.Add(Key, value);
                    valueChanged = true;
                }
            }
            catch (ArgumentException)
            {
                m_isolatedStore.Add(Key, value);
                valueChanged = true;
            }
            catch (Exception e)
            {
                MessageBox.Show("Exception while using IsolatedStorageSettings: " + e.ToString());
            }

            return valueChanged;
        }

        /// <summary>
        /// Get the current value of the setting, or if it is not found, set the 
        /// setting to an empty List.
        /// </summary>
        /// <typeparam name="valueType"></typeparam>
        /// <param name="Key"></param>
        /// <returns></returns>
        public List<DefType> GetListOrDefault<DefType>(string Key)
        {
            try
            {
                if (m_isolatedStore.Contains(Key))
                {
                    return (List<DefType>)m_isolatedStore[Key];
                }
                else
                {
                    return new List<DefType>();
                }
            }
            catch (ArgumentException)
            {
                return new List<DefType>();
            }
            catch (Exception e)
            {
                MessageBox.Show("Exception while using IsolatedStorageSettings: " + e.ToString());
            }

            return new List<DefType>();
        }

        public List<String> Messages
        {
            get
            {
                return this.GetListOrDefault<String>("Messeges");
            }

            set
            {
                this.AddOrUpdateValue("Messeges", value);
                this.Save();
            }
        }

        /// <summary>
        /// Delete all data
        /// </summary>
        public void DeleteAll()
        {
            m_isolatedStore.Clear();
        }

        /// <summary>
        /// Save the settings.
        /// </summary>
        public void Save()
        {
            m_isolatedStore.Save();
        }
    }
}

Then just add to the page

private AppSettings m_appSettings = new AppSettings();

To get the messages from IsolatedStorage:

var messages = m_appSettings.Messages;

to save them:

 m_appSettings.Messages = messages;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM