我尝试使用 WP7 中的独立存储在 txt 文件中保存多行,但它只能保存一行,如何保存多行?
我用它来保存每次游戏结束时的分数
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("");
}
它只对最后一行收费,为什么它不读取超过一行?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
FileMode.Create 每次都会覆盖该文件。您应该使用 FileMode.OpenOrCreate 或 FileMode.Append 代替(参考:http ://msdn.microsoft.com/en-us/library/system.io.filemode.aspx)
另外,请查看独立存储资源管理器工具来验证您的文件:http://msdn.microsoft.com/en-us/库/hh286408(v=VS.92).aspx
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