0

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 に書き込みたい場合、アプリケーションはクラッシュしませんが、失敗します。

なぜなのかご存知ですか?

ありがとう。

4

2 に答える 2

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();
于 2014-11-05T18:56:45.197 に答える
0

You appear to write to the file first, which makes sense, but when you do so you use a file access mode - FileMode.Open - which means "open an existing file". The first time you do this the file won't exist and the open will fail.

You should either use FileMode.OpenOrCreate, which is self explanatory, or FileMode.Append which will open the file if it exists and seek to the end of the file, or create a new file if it doesn't.

If you want to throw away any pre-existing file (which is what your delete then create will do) then just use FileMode.Create

于 2014-11-07T09:47:47.450 に答える