我知道这个问题已被多次询问,但我几乎尝试过所有事情,但没有运气。所以请帮忙。
一些全局变量:
private string _DirectoryForLogin = "LoginFiles", _FileForLogin = "Login.startup";
private IsolatedStorageFile _GlobalAccessRight = IsolatedStorageFile.GetUserStoreForApplication();
_DirectoryForLogin存储存储登录信息的目录名称,_FileForLogin存储保存为.startup的txt文件(我不认为这是问题)。
在InitializeCompolnent()之后,我这样做:
using(_GlobalAccessRight){
//Checking if directory exists; case when user had already been logged in.
if (_GlobalAccessRight.DirectoryExists(_DirectoryForLogin))
{
//Check if login startup file is already created.
if (_GlobalAccessRight.FileExists(_DirectoryForLogin + "\\" + _FileForLogin))
{
//Not of much concern for you people.
_UserNameLabel.Text = "Please wait.... Logging in.";
_UserNameHolder.BorderThickness = new Thickness(0);
_UserNameHolder.IsEnabled = false;
_PasswordLabel.Text = "";
_PasswordHolder.BorderThickness = new Thickness(0);
_PasswordHolder.IsEnabled = false;
_LoginButton.BorderThickness = new Thickness(0);
_LoginButton.Content = "";
_LoginButton.IsEnabled = false;
}
else
_GlobalAccessRight.CreateFile(_DirectoryForLogin + "\\" + _FileForLogin);
//This part basically only creates the file, if directory was already existing.
}
else
{
//Creates directory as well as file.
_GlobalAccessRight.CreateDirectory(_DirectoryForLogin);
_GlobalAccessRight.CreateFile(_DirectoryForLogin + "\\" + _FileForLogin);
}
}
到目前为止一切正常。问题是如果我在创建目录后尝试删除目录,它会向我发送一个错误:“在IsolatedStorage上不允许操作”。但是我暂时解决了这个问题,只是一直关闭我的模拟器,以便获得一个新的IsolatedStorage位置。
接下来,我将从用户的文本和密码框中获取输入,并通过以下代码将其写入上面的文件:
using(_GlobalAccessRight){
//I have also tried by giving the FileShare argument below.
using(IsolatedStorageFileStream WritingStreamForLoginInfo = new IsolatedStorageFileStream(_DirectoryForLogin + "\\" + _FileForLogin, FileMode.OpenOrCreate, FileAccess.ReadWrite, _GlobalAccessRight)){
using(StreamWriter WritingLoginInfo = new StreamWriter(WritingStreamForLoginInfo)){
String _LoginCredentials = "UserName: ";
_LoginCredentials += _UserNameHolder.Text;
_LoginCredentials += "\n";
_LoginCredentials = "Password: ";
_LoginCredentials += _PasswordHolder.Password;
//I added this afterwards. Before it was not here. And still it was not working.
WritingLoginInfo.Flush();
WritingLoginInfo.Write(_LoginCredentials.ToCharArray(), 0, _LoginCredentials.Length);
WritingLoginInfo.Close();
WritingStreamForLoginInfo.Close();
}//End of 1st using clause
}//End of second using clause
}//End of third using clause
上述编写代码在登录按钮的Tap事件上执行的功能中执行。 代码发送此错误: 商店必须为此操作开放。
例外细节:
System.ObjectDisposedException未处理
Message = Store必须为此操作打开。
堆栈跟踪:
at System.IO.IsolatedStorage.IsolatedStorageFileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, IsolatedStorageFile isf)
at System.IO.IsolatedStorage.IsolatedStorageFileStream..ctor(String path, FileMode mode, FileAccess access, IsolatedStorageFile isf)
at SafeMessenger.MainPage._LoginButton_Tap(Object sender, GestureEventArgs e)
at MS.Internal.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args)
at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName)
现在最后一件事我应该告诉你们。在我这样做之前,为每件事创建一个单独的IsolatedStorageFile对象。这是在上面的代码和目录创建代码中。那时我收到了这个错误: “在IsolatedStorage上不允许操作。”
现在我试图查看各种变量的调试细节,我发现这可能对你的人有所帮助。记住_GlobalAccessRight是IsolatedStorageFile类型的全局私有变量。
配额'this._GlobalAccessRight.Quota'抛出类型'System.ObjectDisposedException'的异常long {System.ObjectDisposedException}
请帮助我。感谢您的时间和任何帮助,提前。这将是一个很大的帮助。
答案 0 :(得分:2)
因此,经过阅读和思考,我终于让我的代码独立工作了。 首先是由于我对流的处理不当而引发了不允许操作的错误。
首先,我更改了这些内容:
.
.
.
}
else
_GlobalAccessRight.CreateFile(_DirectoryForLogin + "\\" + _FileForLogin);
//This part basically only creates the file, if directory was already existing.
}
else
{
//Creates directory as well as file.
_GlobalAccessRight.CreateDirectory(_DirectoryForLogin);
_GlobalAccessRight.CreateFile(_DirectoryForLogin + "\\" + _FileForLogin);
}
要:
.
.
.
}
else
_GlobalAccessRight.CreateFile(_DirectoryForLogin + "\\" + _FileForLogin).Dispose();
//This part basically only creates the file, if directory was already existing.
}
else
{
//Creates directory as well as file.
_GlobalAccessRight.CreateDirectory(_DirectoryForLogin);
_GlobalAccessRight.CreateFile(_DirectoryForLogin + "\\" + _FileForLogin).Dispose();
}
第二个错误是: 我全局声明了IsolatedStorageFile对象。在我的第一个使用块中使用它之后:
using(_GlobalAccessRight){
//Checking if directory exists; case when user had already been logged in.
if (_GlobalAccessRight.DirectoryExists(_DirectoryForLogin))
{
。 。 。 。 它被置于最后。 因此,当我在函数中再次使用它时:
using(_GlobalAccessRight){
//I have also tried by giving the FileShare argument below.
using(IsolatedStorageFileStream WritingStreamForLoginInfo = new IsolatedStorageFileStream(_DirectoryForLogin + "\\" + _FileForLogin, FileMode.OpenOrCreate, FileAccess.ReadWrite, _GlobalAccessRight)){
using(StreamWriter WritingLoginInfo = new StreamWriter(WritingStreamForLoginInfo)){
String _LoginCredentials = "UserName: ";
。 。 。 它告诉我错误说明: “商店必须是开放的。”
所以我把它改成了这个:
using(_GlobalAccessRight = IsolatedStorageFile.GetUserStoreForApplication()){
using(IsolatedStorageFileStream WritingStreamForLoginInfo = new IsolatedStorageFileStream(_DirectoryForLogin + "\\" + _FileForLogin, FileMode.OpenOrCreate, FileAccess.ReadWrite, _GlobalAccessRight)){
using(StreamWriter WritingLoginInfo = new StreamWriter(WritingStreamForLoginInfo)){
String _LoginCredentials = "UserName: ";
多数民众赞成!好样的! :)
答案 1 :(得分:0)
尝试删除这两行:
WritingLoginInfo.Close();
WritingStreamForLoginInfo.Close();
using
块应该自动打开和关闭/处理流。可能是由于代码试图在using
块结束时关闭已经关闭的连接而导致错误“Store必须为此操作打开”。