Transform array into text file and store in isolated storage
I am trying to transform all the data inside my array into textfile, so as to read all the item one by one and save it into an isolated storage and displaying it out in another page listbox.
I tried this but i encounter error stating "Operation not permitted on IsolatedStorageFileStream". I need help in this, and please get back to me asap. Thank you!
//save list to favourite list
private void addListBtn_Click(object sender, RoutedEventArgs e)
{
///////for-loop to access userDrinksList
//////transform array into textfile, one line for each drink
///////write in quantity of file into isolated storage using write line function
for (int i = 0; i < (Application.Current as App).userDrinksList.Count; i++)
{
//Obtain the virtual store for application
IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication();
//Create a new folder and call it "ListFolder"
storage.CreateDirectory("ListFolder");
//Create a new file and assign a StreamWriter to the store and this new file (myFile.txt)
//Also take the text contents from the txtWrite control and write it to myFile.txt
// StreamReader readFile = null;
StreamWriter writeFile = new StreamWriter(new IsolatedStorageFileStream("ListFolder\\myFile.txt", FileMode.OpenOrCreate, storage));
StreamReader readFile = new StreamReader(new IsolatedStorageFileStream("ListFolder\\myFile.txt", FileMode.Open, storage));
string myFile = readFile.ReadToEnd();
string[] li开发者_JS百科nes = myFile.Split(System.Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
//write content inside myFile.txt
writeFile.WriteLine(myFile);
//retrieveDrinksListBox.Items.AddRange(lines);
//writeFile.Close();
}
The exception you're seeing is thrown when you attempt to read from the file you just opened for writing:
StreamReader readFile = new StreamReader(new IsolatedStorageFileStream("ListFolder\\myFile.txt", FileMode.Open, storage
));
The problem is also that you never close the file when you've read it (Although you haven't gotten far enough in the code to have an issue with that). All the code you have is good, you just need to reorder it. Try
StreamReader readFile = new StreamReader(new IsolatedStorageFileStream("ListFolder\\myFile.txt", FileMode.Open, storage));
string myFile = readFile.ReadToEnd();
readFile.Close();
//Create a new file and assign a StreamWriter to the store and this new file (myFile.txt)
//Also take the text contents from the txtWrite control and write it to myFile.txt
// StreamReader readFile = null;
StreamWriter writeFile = new StreamWriter(new IsolatedStorageFileStream("ListFolder\\myFile.txt", FileMode.OpenOrCreate, storage));
string[] lines = myFile.Split(System.Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
//write content inside myFile.txt
writeFile.WriteLine(myFile);
//retrieveDrinksListBox.Items.AddRange(lines);
writeFile.Close();
Can you give a little more information on the exception being thrown? By reading your code it looks like you are trying to append to the file. You can eliminate the StreamReader
by simply setting the FileMode
to Append
during the creation of the StreamWriter
. See below:
StreamWriter writeFile = new StreamWriter(
new IsolatedStorageFileStream("ListFolder\\myFile.txt", FileMode.Append, storage));
FileMode.Append
will either append to the file if it exists, or create a new file.
Also, I'm not sure if you intended the line writeFile.WriteLine(myFile)
to write myFile
rather than the what is stored in the lines
variable (your data).
精彩评论