我正在设置一个设置页面,用户可以从listpicker控件中选择问题,歌曲和合格率。
然后将所选问题,歌曲和合格率的索引写入隔离存储。
以下是我的代码:
int indexQues;
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\\rateSetting.txt"))
{
myIsolatedStorage.DeleteFile("SettingFolder\\rateSetting.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\\rateSetting.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未处理” 访问IsolatedStorage时发生错误。
答案 0 :(得分:2)
我不确定这是否是您问题的原因,但我从未使用StreamWriter
写入隔离存储。尝试将编写代码更改为以下内容:
//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\\rateSetting.txt",
FileMode.Append, myStore)) ) {
byte[] rateBytes = UTF8Encoding.UTF8.GetBytes( rate );
isoStream.Write(rateBytes, 0, rateBytes.Length);
isoStream.Flush();
}
答案 1 :(得分:0)