关于isolatedstorage:windows phone 7 中的孤立存储异常

Isolated storage exception in windows phone 7

我正在尝试做一个设置页面,用户可以从列表选择器控件中选择问题、歌曲和通过率的数量..

然后将选中的题目、歌曲和通过率的索引写入隔离存储。

下面是我的代码:

int indexQues;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
    string rate;
    private void saveBtn_Click(object sender, RoutedEventArgs e)
    {
        using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
        {
            if (myIsolatedStorage.FileExists("SettingFolder\\\\queSetting.txt"))
            {
                myIsolatedStorage.DeleteFile("SettingFolder\\\\queSetting.txt");
            }

            if (myIsolatedStorage.FileExists("SettingFolder\\\
ateSetting.txt"))
            {
                myIsolatedStorage.DeleteFile("SettingFolder\\\
ateSetting.txt");
            }
        }

        indexQues = queListPicker.SelectedIndex;
        rate = rateListPicker.SelectedItem.ToString();

        //Save the number of question to answer when the alarm ring
        //Obtain the virtual store for application
        IsolatedStorageFile myStore = IsolatedStorageFile.GetUserStoreForApplication();
        IsolatedStorageFile myStore1 = IsolatedStorageFile.GetUserStoreForApplication();
        //Create a new folder and call it"AlarmFolder"
        myStore.CreateDirectory("SettingFolder");

        //Retrieve the content of"noOfQues"
        //And write it into queSetting.txt
        StreamWriter writeFile = new StreamWriter(new IsolatedStorageFileStream("SettingFolder\\\\queSetting.txt", FileMode.Append, myStore));
        StreamWriter writeFile1 = new StreamWriter(new IsolatedStorageFileStream("SettingFolder\\\
ateSetting.txt", FileMode.Append, myStore));

        writeFile.Write(indexQues);
        writeFile1.Write(rate);
        writeFile.Close();
        writeFile1.Close();

        MessageBox.Show("Setting Saved");
        NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
    }

上面的代码允许我写入隔离存储,但是当我第三次尝试保存时出现错误。

错误是"IsolatedStorageException 未处理"
访问独立存储时出错。


我不确定这是否是您的问题的原因,但我从未使用 StreamWriter 写入隔离存储。尝试将编写代码更改为以下内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
//Save the number of question to answer when the alarm ring
//Obtain the virtual store for application
IsolatedStorageFile myStore = IsolatedStorageFile.GetUserStoreForApplication();

//Create a new folder and call it"AlarmFolder"
myStore.CreateDirectory("SettingFolder");

//Retrieve the content of"noOfQues"
//And write it into queSetting.txt
using( IsolatedStorageFileStream isoStream =
  new IsolatedStorageFileStream("SettingFolder\\\\queSetting.txt",
    FileMode.Append, myStore)) ) {

      byte[] indexBytes = UTF8Encoding.UTF8.GetBytes( indexQues.ToString() );

      isoStream.Write(indexBytes, 0, indexBytes.Length);
      isoStream.Flush();
}

using( IsolatedStorageFileStream isoStream =
  new IsolatedStorageFileStream("SettingFolder\\\
ateSetting.txt",
    FileMode.Append, myStore)) ) {

      byte[] rateBytes = UTF8Encoding.UTF8.GetBytes( rate );

      isoStream.Write(rateBytes, 0, rateBytes.Length);
      isoStream.Flush();
}


这里有很多很好的例子和演练,教会了我很多!

http://channel9.msdn.com/blogs/egibson/windows-phone-7-jump-start-session-7-of-12-advanced-application-development-part-1