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 回答