0

每次比赛结束时我都用它来保存分数

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

并从 txt 中读取,当读数为空时,我使用 for 停止

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

它只对最后一行收费,为什么它读不到多行?

4

1 回答 1

2

FileMode.Create 每次都会覆盖文件。您应该改用 FileMode.OpenOrCreate 或 FileMode.Append(参考:http: //msdn.microsoft.com/en-us/library/system.io.filemode.aspx

此外,请查看独立存储资源管理器工具以验证您的文件: http: //msdn.microsoft.com/en-us/library/hh286408 (v=VS.92).aspx

于 2011-09-23T04:49:15.937 回答