[英]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.