0
votes

I am trying to do a setting page where the user can choose the number of question, song and passing rate from listpicker control..

Then the selected index of the question, song and passing rate will be write into isolated storage.

Below is my code:

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));
    }

The above code allow me to write into the isolated storage but there is a error when i trying to save for the third time.

The error was "IsolatedStorageException was unhandled" An error occurred while accessing IsolatedStorage.

2

2 Answers

2
votes

I'm not sure whether this is the cause of your problem but I've never used StreamWriter to write to isolated storage. Try changing the writing code to the following:

//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();
}