开发者

Checking and editing a value in isolated storage in windows phone 7

开发者 https://www.devze.com 2023-03-24 16:39 出处:网络
I am trying to check for the word \"Alarm\" in my isolated storage before saving. If the word \"Alarm\" is exist i will change the \"Alarm\" to \"Alarm1\" then if \"Alarm1\" is exist will change to \

I am trying to check for the word "Alarm" in my isolated storage before saving.

If the word "Alarm" is exist i will change the "Alarm" to "Alarm1" then if "Alarm1" is exist will change to "Alarm2".

How should i go about doing it?

Below is my code but it is not working:

if (labelTextB开发者_JAVA技巧ox.Text == "")
{
    try
    {
        using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
        {

            foreach (string label in storage.GetFileNames("*"))
            {
                MessageBox.Show(label);
            }
        }
    }
    catch (Exception)
    {
    }

    int i = 0;
    i++;
    labelTextBox.Text = "Alarm" + i;
    alarmLabel = (labelTextBox.Text.ToString()).Replace(" ", "_");
}


You could use the IsolatedStorageSettings.ApplicationSettings, which is more appropriated for object (e.r. string) handling.

I made a small sample which makes use of this class:

using System;
using System.IO.IsolatedStorage;
using System.Windows;
using Microsoft.Phone.Controls;

namespace SidekickWP7
{
    public partial class Page1 : PhoneApplicationPage
    {
        const string MYALARM = "MyAlarm";

        public Page1()
        {
            InitializeComponent();

            Loaded += new RoutedEventHandler(Page1_Loaded);
        }

        void Page1_Loaded(object sender, RoutedEventArgs e)
        {
            int intAlarm = 0;

            Int32.TryParse(Load(MYALARM).ToString(), out intAlarm);

            intAlarm++;

            MessageBox.Show(intAlarm.ToString());

            Save(MYALARM, intAlarm);
        }

        private static object Load(string strKey)
        {
            object objValue;

            if (IsolatedStorageSettings.ApplicationSettings.TryGetValue<object>(strKey, out objValue) == false)
            {
                objValue = String.Empty;
            }

            return objValue;
        }

        private static void Save(string strKey, object objValue)
        {
            IsolatedStorageSettings.ApplicationSettings[strKey] = objValue;

            IsolatedStorageSettings.ApplicationSettings.Save();
        }
    }
}


try this:

using (var store = IsolatedStorageFile.GetUserStoreForApplication())
{
    int highestNumberFound = -1;

    foreach (var fileName in store.GetFileNames())
    {
        if (fileName.StartsWith("alarm"))
        {
            if (fileName == "alarm")
            {
                if (highestNumberFound < 0)
                {
                    highestNumberFound = 0;
                }
            }
            else if (fileName.Length > 5)
            {
                int numb;

                if (int.TryParse(fileName.Substring(5), out numb))
                {
                    if (numb > highestNumberFound)
                    {
                        highestNumberFound = numb;
                    }
                }
            }
        }
    }

    string toCreate = "alarm";

    if (++highestNumberFound > 0)
    {
        toCreate += highestNumberFound.ToString();
    }

    store.CreateFile(toCreate);
}

Not pretty but it should work.

I storngly suspect that creating empty files with different names isn't the best way to achieve whatever it is that you're trying to do though.

0

精彩评论

暂无评论...
验证码 换一张
取 消