Storing more than one value into Isolated Storage

140 views Asked by At

I have created a isolated storage that only store one values and display one value of e.g. "aaaaaaaa,aaaaaaaa".
How can I make it so that it stores
1)"aaaaaaaa,aaaaaaaaa"
2)"bbbbbbbb,bbbbbbbbb"
3)"cccccccccc,cccccccccc"

Below shows codes that stores only one value:

//get the storage for your app 
IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();
//define a StreamWriter
StreamWriter writeFile;

if (!store.DirectoryExists("SaveFolder"))
{
    //Create a directory folder
    store.CreateDirectory("SaveFolder");
    //Create a new file and use a StreamWriter to the store a new file in 
    //the directory we just created
    writeFile = new StreamWriter(new IsolatedStorageFileStream(
         "SaveFolder\\SavedFile.txt", 
         FileMode.CreateNew, 
         store));
}
else
{
    //Create a new file and use a StreamWriter to the store a new file in 
    //the directory we just created
    writeFile = new StreamWriter(new IsolatedStorageFileStream(
         "SaveFolder\\SavedFile.txt", 
         FileMode.Append, 
         store));
}

StringWriter str = new StringWriter();
str.Write(textBox1.Text);
str.Write(",");
str.Write(textBox2.Text);

writeFile.WriteLine(str.ToString());
writeFile.Close();

textBox1.Text = string.Empty;
textBox2.Text = string.Empty;

    StringWriter str = new StringWriter();
str.Write(textBox1.Text);
str.Write(",");
str.Write(textBox2.Text);


writeFile.WriteLine(str.ToString());
writeFile.Close();

textBox1.Text = string.Empty;
textBox2.Text = string.Empty;       
2

There are 2 answers

0
Damith On BEST ANSWER

You can use WriteLine several times like below

writeFile.WriteLine(string.Format("{0},{1}", "aaaaa","aaaaa"));
writeFile.WriteLine(string.Format("{0},{1}", "bbbbb","bbbbb"));
writeFile.WriteLine(string.Format("{0},{1}", "ccccc","ccccc"));
1
Algamest On

I can confirm Damith's answer, and should be marked as so.

If you're wondering about reading the lines back in:

IsolatedStorageFileStream stream = new IsolatedStorageFileStream("SaveFolder", FileMode.Open, store);

StreamReader reader = new StreamReader(stream);

string rdr = reader.ReadLine();

rdr will be a string representing the first line, you can then call ReadLine() again to get the next line, etc. The first call to ReadLine() will return the first line you wrote to the store.

Hope this is useful to you or anyone else working in this area.