Frage

I know this question has been asked many times, but I have almost tried everything but got no luck. So please help.

Some global variables:

private string _DirectoryForLogin = "LoginFiles", _FileForLogin = "Login.startup";
private IsolatedStorageFile _GlobalAccessRight = IsolatedStorageFile.GetUserStoreForApplication();

_DirectoryForLogin stores the name of the directory that stores login info and _FileForLogin stores txt file saved as .startup(I do not think this is the problem).

Later after InitializeCompolnent() I am doing this:

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);

            }
        }

Things were working fine till now. Problem is that if I try to delete the directory anywhere after creating directory, it sends me an error: "Operation not permitted on IsolatedStorage." But I resolved it for time being by just closing my emulator all the time so that I get a fresh IsolatedStorage location.

Next I am taking input from the user in the text and password boxes, and writing them on a file above through this code:

        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

The above writing code is executed in the function that is executed on a Tap event of a Login button. The code is sending this error: Store must be open for this operation.

Exception detail:

System.ObjectDisposedException was unhandled

Message=Store must be open for this operation.

StackTrace:

   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)

Now one last thing that I should tell you people. Before I was doing like, creating a separate IsolatedStorageFile object for each thing. That is in above code, and in directory creation code. At that time I was getting this error: "Operation not permitted on IsolatedStorage."

Right now I tried to go through the debug details of various variables and I found this which could be of some help for you people. Remember _GlobalAccessRight is a global private variable of IsolatedStorageFile type.

Quota 'this._GlobalAccessRight.Quota' threw an exception of type 'System.ObjectDisposedException' long {System.ObjectDisposedException}

Kindly help me with this. Thank you for your time and any help, in advance. It would be a great help.

War es hilfreich?

Lösung

So after reading and thinking a lot I have finally got my code working on my own. First the errors that operation not permitted were raised because of my bad handling of streams.

First I changed these lines:

.
.
.
}
 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);
    }

To:

.
.
.
}
 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();
    }

The second mistake was this: I globally declared the IsolatedStorageFile object. After using it in my First using block:

using(_GlobalAccessRight){
        //Checking if directory exists; case when user had already been logged in.
        if (_GlobalAccessRight.DirectoryExists(_DirectoryForLogin))
        {

. . . . It got disposed at the end of it. Therefore when I used it again in the function:

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: ";

. . . it threw me error stating: "Store must be open for that."

So I changed it to be this:

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: ";

Thats it! Bravo! :)

Andere Tipps

Try to remove these two lines:

WritingLoginInfo.Close();
WritingStreamForLoginInfo.Close();

using block supposed to open and close/dispose stream automatically. Maybe the error "Store must be open for this operation" caused by the code trying to close already closed connection at the end of using block.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top