Resolved Reading and Writing to a Isolated Storage File

jushymaso222

New member
Joined
Feb 5, 2023
Messages
3
Programming Experience
3-5
Hello, I'm trying to write a basic profile handler for my winforms application. All the handler itself needs to be able to do is login and out of profiles, as well as read/write data from an isoStore. I feel like I'm super close however, whenever I go to run the app, I get an error saying the file is already open when calling the 'isoStream' object under the 'WriteData' method. Here's the code for the profile, I'm still a little new to C# so it may be a little messy in general but for the most part, all of it runs except for the ReadData and WriteData methods. The only other thing you really need for reference on this is when the class is called in Main().

Error:
System.IO.IOException: 'The process cannot access the file 'Users/thejohnsmith.txt' because it is being used by another process.'

Code:
-----------------------------------------------------------------------------------------------------------------

Profile Class:
using System;
using System.Collections.Generic;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Runtime.InteropServices;
using System.Xml.Serialization;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel;

namespace Designing
{
    internal class UserData
    {
        //Create Isolated Storage Reference and Initialize Serializer
        IsolatedStorageFile isoStore = IsolatedStorageFile.GetMachineStoreForAssembly();
        XmlSerializer serializer = new XmlSerializer(typeof(LanguageSettings<string, string>));

        //Private Properties
        public string Username { get; set; }
        private string Password { get; set; }
        private string Email { get; set; }
        private string FullName { get; set; }
        private Dictionary<string, bool> Settings { get; set; }

        //Methods
        public UserData(string username, string password, string email, string fullname)
        {
            this.Username = username;
            this.Password = password;
            this.Email = email;
            this.FullName = fullname;
            this.Settings = new Dictionary<string, bool>
                (
                
                
                );
            WriteData();
        }

        public bool LogIn(string password, string username = "none", string email = "none")
        {
            ReadData();
            if (username == "none" && email == "none")
            {
                return false;
            }
            else
            {
                string inputUser = (username == "none") ? username : email;


                return true;
            }
        }

        private void WriteData()
        {
            if (isoStore.DirectoryExists("Users"))
            {
                isoStore.CreateFile($"Users/{this.Username}.txt");
            }
            else
            {
                isoStore.CreateDirectory("Users");
                isoStore.CreateFile($"Users/{this.Username}.txt");
            }
            
            using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream($"Users/{this.Username}.txt", FileMode.Open, isoStore))
            {
                using (StreamWriter writer = new StreamWriter(isoStream))
                {
                    Dictionary<string, string> allUserData = new Dictionary<string, string>();
                    allUserData["username"] = this.Username;
                    allUserData["password"] = this.Password;
                    allUserData["email"] = this.Email;
                    allUserData["fullName"] = this.FullName;
                    serializer.Serialize(writer, allUserData);
                    isoStream.Close();
                }
            }
            using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream($"Users/{this.Username}Settings.txt", FileMode.Open, isoStore))
            {
                using (StreamWriter writer = new StreamWriter(isoStream))
                {
                    Dictionary<string, string> userSettingsData = new Dictionary<string, string>();
                    foreach (KeyValuePair<string, bool> entry in this.Settings)
                    {
                        userSettingsData[entry.Key] = Convert.ToString(entry.Value);
                        serializer.Serialize(writer, userSettingsData);
                        isoStream.Close();
                    }
                }
            }
        }

        private void ReadData()
        {
            using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream($"Users/{this.Username}.txt", FileMode.Open, isoStore))
            {
                using (StreamReader reader = new StreamReader(isoStream))
                {
                    Dictionary<string, string> allUserData =  (Dictionary<string, string>)serializer.Deserialize(reader);
                    Username = allUserData["username"];
                    Password = allUserData["password"];
                    Email = allUserData["email"];
                    FullName = allUserData["fullName"];
                    isoStream.Close();
                }
            }
            using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream($"Users/{this.Username}Settings.txt", FileMode.Open, isoStore))
            {
                using (StreamReader reader = new StreamReader(isoStream))
                {
                    Dictionary<string, string> userSettingsData = (Dictionary<string, string>)serializer.Deserialize(reader);
                    Dictionary<string, bool> userSettingsFormatted = new Dictionary<string, bool>();
                    foreach (KeyValuePair<string, string> entry in userSettingsData)
                    {
                        userSettingsFormatted[entry.Key] = Convert.ToBoolean(entry.Value);
                    }
                    Settings = userSettingsFormatted;
                    isoStream.Close();
                }
            }
        }

    }
}

-----------------------------------------------------------------------------------------------------------------


The entire handler is initialized with this line:
------------------------------------------------------------------

Class Creation:
userData = new UserData("thejohnsmith","password","jsmith@gmail.com","John Smith");
------------------------------------------------------------------

Any help is greatly appreciated, thank you!
 
On line 67, when you created the file, it returns a stream and leaves the file open.
 
Solution
No. Close the returned stream.
 
Since you know how to use using, you could have also use using on the create for call.
 
Back
Top Bottom