简体   繁体   中英

I try to save many lines in a txt file using isolated storage in WP7 but it will only saves one, how do I save many?

I use this to save the score everytime the game is over

IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication();
isf.CreateDirectory("Data");
StreamWriter sw = new StreamWriter(new IsolatedStorageFileStream("Data\\scoretest.txt", FileMode.Create, isf));
sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm") + "                                   Score: " + punc);
sw.Close();

and to read from the txt I use a for that stops when the reading is null

IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication();
StreamReader sr = null;
try
{
    sr = new StreamReader(new IsolatedStorageFileStream(@"Data\scoretest.txt", FileMode.Open, isf));
    for (;;)
    {
        string x = sr.ReadLine();
        if (x == null)
           break;
        else
            listBox1.Items.Add(x);
    }
    sr.Close();
}
catch
{
    listBox1.Items.Add("");
}

It only charges the last line, Why it doesn't read more than one line?

FileMode.Create will overwrite the file every time. You should use FileMode.OpenOrCreate or FileMode.Append instead(ref:http://msdn.microsoft.com/en-us/library/system.io.filemode.aspx )

Also, take a look at the Isolated Storage Explorer tool to verify your file: http://msdn.microsoft.com/en-us/library/hh286408(v=VS.92).aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM