C# IsolatedStorage文件流抛出“;IsolatedStorageExection未经处理“;
基本上,我正在编写代码,在IsolatedStore中创建一个新文件,我确信我正确地关闭了流,但肯定有什么遗漏了,你能看看它,确保我没有遗漏什么明显的东西吗 这是我的保存函数,在这里抛出错误,并在游戏结束时在用户输入其姓名后调用:C# IsolatedStorage文件流抛出“;IsolatedStorageExection未经处理“;,c#,windows-phone-7.1,text-files,xna-4.0,isolatedstorage,C#,Windows Phone 7.1,Text Files,Xna 4.0,Isolatedstorage,基本上,我正在编写代码,在IsolatedStore中创建一个新文件,我确信我正确地关闭了流,但肯定有什么遗漏了,你能看看它,确保我没有遗漏什么明显的东西吗 这是我的保存函数,在这里抛出错误,并在游戏结束时在用户输入其姓名后调用: public void SaveHighScores(string NewName, int NewScore) { SortHighScores(NewName, NewScore); IsolatedStorageFile isoStore =
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
}
}
有人能解释一下吗
[编辑]
好吧,由于某种原因,现在我在新安装的应用程序上第一次调用“保存游戏”功能时,它就可以工作了,但是下次我玩游戏时,或者当我重新启动游戏并再次玩时,它在尝试保存时崩溃了,这很奇怪,是FileMode.CreateNew
可能是我出了什么问题
[编辑]
是的,FileMode.CreateNew
在文件已经存在时抛出一个exeption,所以我在创建新文件之前添加了isoStore.DeleteFile(“HighScores.txt”)
,现在就像做梦一样工作:)
[已解决]
FileMode.CreateNew
在文件已存在时引发异常,因此必须调用isoStore.DeleteFile(字符串文件名)
在创建新文件之前,或者如果您不一定需要使用文件模式,也可以选择使用文件模式。CreateNew
使用文件模式。Create
改为您是否尝试过使用Try Catch?是在运行时还是在编译时抛出错误?运行时,我会在吃完晚餐后立即尝试添加一个try-catch刚刚更新了我的帖子,现在抛出exceptionUse文件模式时出错。请改为创建。
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();
}
}
}