麻烦与IsolatedStorageFileStream
问题描述:
每当我运行此代码:麻烦与IsolatedStorageFileStream
IsolatedStorageFile fileStorage = IsolatedStorageFile.GetUserStoreForApplication();
StreamWriter Writer = new StreamWriter(new IsolatedStorageFileStream("TestFile.txt", FileMode.OpenOrCreate, fileStorage));
Writer.WriteLine(email1.Text + "," + email2.Text + "," + email3.Text + "," + email4.Text);
Writer.Close();
我得到这个错误:
An exception of type 'System.IO.IsolatedStorage.IsolatedStorageException' occurred in mscorlib.ni.dll but was not handled in user code
我使用模拟器但是这不应该是一个问题。我已经包括了线
Using System.IO.IsolatedStorage;
答
非常样的问题,你在使用IsolatedStorage是
System.IO.IsolatedStorage.IsolatedStorageException
那是因为你没有实际关闭存储使用it.This将引发异常后在安全方面也。将代码重写为:
using (var storage = IsolatedStorageFile.GetUserStoreForApplication())
{
using (var file = storage.OpenFile("TestFile.txt", System.IO.FileMode.OpenOrCreate))
{
using (System.IO.StreamWriter writer = new System.IO.StreamWriter())
{
writer.WriteLine(email1.Text + "," + email2.Text + "," + email3.Text + "," + email4.Text);
}
}
}
实际使用的确实是使用将会调用使其可重用的dispose方法。存储,文件流,Streamwriter都配置了实际使用“使用”的方法。这通常不会引发维护资源的异常,但对于文件名的参数异常仍然会产生问题。
Try...catch must be used always while handling files and input.
编辑 守则如何阅读:
string dataToRead = string.Empty;
using (var storage = IsolatedStorageFile.GetUserStoreForApplication())
{
using (var file = storage.OpenFile("TestFile.txt", System.IO.FileMode.Open))
{
using (var reader = new System.IO.StreamReader(file))
{
dataToRead = reader.ReadToEnd();
}
}
}
裹在'再... Catch'块中的代码,看到了异常消息说什么。 – keyboardP
@keyboardP异常消息将关于IsolatedStorageException权限。它从调试器消息给这个有什么不同?它只关于他如何处理变量? – Mani
@max - 在这种情况下,你正确地关闭了'Stream'(+1),但OP对于'Try ... Catch'方法有很好的了解,以及如何使用它们来帮助调试问题。 – keyboardP