[英]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?
每次比赛结束时我都用它来保存分数
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("");
}
它只对最后一行收费,为什么它读不到多行?
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.