2011-08-03 29 views
1

我正在嘗試在保存前檢查隔離存儲中的「報警」一詞。檢查並編輯windows phone 7中的隔離存儲中的值

如果存在「報警」這個詞,我會將「報警」更改爲「報警1」,那麼如果「報警1」存在,則將更改爲「報警2」。

我該如何去做呢?

下面是我的代碼,但它不工作:

if (labelTextBox.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(" ", "_"); 
} 
+0

您是否在查看名爲「Alarm」的文件是否存在?或者如果IS中的任何文件包含文本「報警」? –

+0

Ya存在的任何「警報」 –

+0

但是,如果存在「警報」,則下一個將會是「alarm1」以替換它,依此類推 –

回答

0

您可以使用IsolatedStorageSettings.ApplicationSettings,這是更撥付對象(E.R.字符串)處理。

我做了一個小樣本,這使得使用這個類:

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(); 
     } 
    } 
} 
0

試試這個:

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); 
} 

不漂亮,但它應該工作。

我強烈懷疑用不同的名字創建空文件並不是實現你想要做的任何事情的最好方式。

+0

嗨,我已經嘗試過了。但它也會觸發警報1並繼續重複警報1。它沒有去警報2,警報3 –

+0

@ben聽起來似乎原始文件沒有被刪除然後。你做了什麼來調試這個問題? –

+0

嗯,我不想刪除原始文件。我想要的東西,如果有一個文件名爲「警報」書房我的下一個文件將被命名爲「alarm1」而不是。原來的「警報」仍然存在。對不起 –

相關問題