基本上我正在编写在IsolatedStorage中创建一个新文件的代码,我确信我正确关闭了这些流,但必须有某些东西丢失,您能否看看在此以确保我不会错过某些明显的东西?IsolatedStorageFileStream投掷“IsolatedStorageExeption was unhandled”
这是我保存功能,它是在错误被抛出,被称为在比赛结束时,一旦用户输入他/她的名字:
public void SaveHighScores(string NewName, int NewScore)
{
SortHighScores(NewName, NewScore);
IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();
try
{
using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream("HighScores.txt", FileMode.CreateNew, isoStore))
{
using (StreamWriter writer = new StreamWriter(isoStream))
{
for (int i = 0; i < 5; i++)
{
writer.WriteLine(MyScores[i].Name);
writer.WriteLine(MyScores[i].Score);
}
writer.Close();
}
isoStream.Close();
}
}
catch (IsolatedStorageException e)
{
throw e; // "IsolatedStorageException was unhandled" error now occurs here
}
}
这里是我的读取功能,它被称为在初始化过程中一次在游戏开始时:
public void ReadHighScores()
{
IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();
if (isoStore.FileExists("HighScores.txt"))
{
try
{
using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream("HighScores.txt", FileMode.Open, isoStore))
{
using (StreamReader reader = new StreamReader(isoStream))
{
int i = 0;
while (!reader.EndOfStream)
{
MyScores[i].Name = reader.ReadLine();
string scoreString = reader.ReadLine();
MyScores[i].Score = Convert.ToInt32(scoreString);
i++;
}
reader.Close();
}
isoStream.Close();
}
}
catch (IsolatedStorageException e)
{
throw e;
}
}
else
{
if (!failedRead)
{
failedRead = true;
ReadHighScores();
}
}
}
任何人都可以对此有所了解吗?
[编辑]
好了,所以由于某种原因,现在这个工作我第一次调用保存在一个新的游戏功能安装应用程序的,但下一次我玩或当我重新启动游戏和娱乐当它试图保存时,它又崩溃了,这很奇怪,是FileMode.CreateNew
,可能是我错了?
[编辑]
是啊,FileMode.CreateNew
抛出exeption当一个文件已经存在,所以我说isoStore.DeleteFile("HighScores.txt")
之前,我创建新的文件,现在就像一个梦:)
[解决]
你有没有尝试过尝试抓住它?它是否在运行时或编译时抛出错误? – 2013-02-12 18:27:47
运行时间,我会尽快尝试添加一个尝试赶上 – TotalJargon 2013-02-12 18:30:22
刚刚更新我的帖子,错误现在发生在我抛出异常时 – TotalJargon 2013-02-12 19:40:38