0

我正在從IsolatedStorage中讀取數據,但無法在ScheduledTask中對其進行編輯。我如何編輯它?Windows Phone中的IsolatedStorage編輯ScheduledTask

private void StartToastTask(ScheduledTask task) 
    { 
     long rank = 0, difference = 0; 
     string text = "", nickname = ""; 
     PishtiWCF.PishtiWCFServiceClient ws = ServiceClass.GetPishtiWCFSvc(); 
     ws.GetUsersRankCompleted += (src, e) => 
     { 
      try 
      { 
       if (e.Error == null) 
       { 
        difference = rank - e.Result.GeneralRank; 
        if (!String.IsNullOrEmpty(nickname)) 
        { 
         if (difference < 0) 
          text = string.Format("{0}, {1} kişi seni geçti!", nickname, difference.ToString(), e.Result.GeneralRank); 
         else if (difference > 0) 
          text = string.Format("{0}, {1} kişiyi daha geçtin!", nickname, Math.Abs(difference).ToString(), e.Result.GeneralRank); 
         else if (e.Result.GeneralRank != 1) 
          text = string.Format("{0}, sıralamadaki yerin değişmedi!", nickname, e.Result.GeneralRank); 
         else 
          text = string.Format("{0}, en büyük sensin, böyle devam!", nickname); 
        } 
        else 
         return; 
        Mutex mut; 
        if (!Mutex.TryOpenExisting("IsoStorageMutex", out mut)) 
         mut = new Mutex(false, "IsoStorageMutex"); 
        mut.WaitOne(); 
        using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication()) 
        { 
         using (IsolatedStorageFileStream stream = file.OpenFile("UserRanks", FileMode.Open, FileAccess.Write)) 
         { 
          StreamWriter writer = new StreamWriter(stream); 
          writer.Write(string.Format("{0},{1}", nickname, e.Result.GeneralRank)); 
          writer.Close(); 
          stream.Close(); 
         } 
        } 
        mut.ReleaseMutex(); 

        ShellToast toast = new ShellToast(); 
        toast.Title = "Pishti"; 
        toast.Content = text; 
        toast.Show(); 
       } 
       FinishTask(task); 
      } 
      catch (Exception) 
      { 

      } 
     }; 
     try 
     { 
      Mutex mut; 
      if (!Mutex.TryOpenExisting("IsoStorageMutex", out mut)) 
       mut = new Mutex(false, "IsoStorageMutex"); 
      mut.WaitOne(); 
      using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication()) 
      { 
       using (IsolatedStorageFileStream stream = file.OpenFile("UserRanks", FileMode.Open, FileAccess.Read)) 
       { 
        using (StreamReader reader = new StreamReader(stream)) 
        { 
         string temp = reader.ReadToEnd(); 
         if (temp.Split(',').Count() > 1) 
         { 
          nickname = temp.Split(',')[0]; 
          rank = long.Parse(temp.Split(',')[1]); 
          ws.GetUsersRankAsync(nickname); 
         } 
         reader.Close(); 
        } 
        stream.Close(); 
       } 
      } 
      mut.ReleaseMutex(); 
     } 
     catch (Exception) 
     { 
     } 

    } 

我正在從UserRanks文件級別,例如1200,但是當我和數據從WCF,編輯它以1000並希望將其寫入IsolatedStorage,它不會崩潰的應用程序,但它失敗。

你知道爲什麼嗎?

謝謝。

+0

是你的catch塊空的一個原因?我看到你解決了你的問題,但你表示它不會崩潰但失敗。如果catch塊爲空,則將指示將指示從何處開始故障排除的異常被吞下。一個好的開始就是將catch(Exception)改爲catch(Exception ex),並將一個斷點放在下一行。這樣,即使您計劃忽略異常,但在調試過程中發生異常時,應用程序將停止並允許您檢查它。 – 2014-11-07 00:08:47

回答

1

我已經用刪除文件修復了它。

    Mutex mut; 
        if (!Mutex.TryOpenExisting("IsoStorageMutex", out mut)) 
         mut = new Mutex(false, "IsoStorageMutex"); 
        mut.WaitOne(); 
        using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication()) 
        { 
         if (file.FileExists("UserRanks")) 
          file.DeleteFile("UserRanks"); 
         using (IsolatedStorageFileStream stream = file.OpenFile("UserRanks", FileMode.OpenOrCreate, FileAccess.Write)) 
         { 
          StreamWriter writer = new StreamWriter(stream); 
          writer.Write(string.Format("{0},{1}", nickname, e.Result.GeneralRank)); 
          writer.Close(); 
          stream.Close(); 
         } 

        } 
        mut.ReleaseMutex(); 
0

你似乎寫入到文件第一,這是有道理的,但是當你做,你使用文件訪問模式 - FileMode.Open - 這意味着「打開現有文件」。第一次你做這個文件將不存在,並打開將失敗。

您應該使用FileMode.OpenOrCreate(自解釋)或FileMode.Append(如果該文件存在並打開文件結尾)或創建一個新文件(如果沒有)。

如果你想扔掉任何預先存在的文件(這是你的刪除,然後建立將做),那麼只需使用FileMode.Create

相關問題