2011-08-03 3 views
1

저장하기 전에 격리 된 저장소에서 "알람"이라는 단어를 확인하려고합니다.Windows 전화에서 격리 된 저장소의 값 확인 및 편집 7

"Alarm"이라는 단어가 있으면 "Alarm1"로 변경하고 "Alarm1"이 있으면 "Alarm2"로 변경됩니다.

어떻게해야합니까?

다음은 내 코드이지만 작동하지 않습니다 :

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의 파일 중 "Alarm"이라는 텍스트가 포함되어 있습니까? –

+0

예요 "alarm"이 존재합니다 –

+0

"alarm"이 존재하면 그 다음에 "alarm1"이 나타나 대체 할 것입니다. –

답변

0

좀 더 객체 (응급 요원 문자열) 처리를 위해 충당하는 IsolatedStorageSettings.ApplicationSettings을 사용할 수 있습니다.

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로 계속 반복합니다. 그것은 did not가 alarm2에 올라간다, alarm3 –

+0

@ben 그것은 본래 파일이 삭제되지 않고있는 likte 소리를 낸다. 문제를 디버깅하기 위해 무엇을 했습니까? –

+0

흠, 원래 파일을 삭제하고 싶지 않았습니다. 나는 "alarm"이라는 이름의 파일이있는 것과 같은 것을 원했다. 다음 파일은 "alarm1"로 이름 지어 질 것이다. 원래의 "알람"은 여전히있을 것입니다. 죄송합니다. –

관련 문제