-3

I have a project and i have to save all my data in text files.
One of those files is users.txt which contains all my users name
and password.

What i want to do is to read all my records from users.txt , and save them
in List of user, this process should be done when project starts.

The list could be increased or decreased depending on the processes of the project (Add User OR Delete User).

After that i want to save my list to the same file when the project is closed
(or when closing the program)

I hop it is clear to understand the idea of the project.

This my github repo which contain the project code.

https://github.com/HeshamRashwanAM/Online-Book-Store-Project

User : is a class i have created and it has string username, string password and list of type user same as class name.

Hesham
  • 3
  • 4
  • 5
    What have you tried so far ? – Jviaches Mar 15 '19 at 19:23
  • https://support.microsoft.com/en-us/help/816149/how-to-read-from-and-write-to-a-text-file-by-using-visual-c This should have some detail on how to accomplish this. – Dortimer Mar 15 '19 at 19:26
  • Possible duplicate of [How best to read a File into List](https://stackoverflow.com/questions/6904401/how-best-to-read-a-file-into-liststring) – gunr2171 Mar 15 '19 at 19:27
  • 1
    A text file is not a database... I suggest using something like an SQLite file. – Nyerguds Mar 15 '19 at 19:31
  • what i want to know is how to read when program starts and how to write when program exit. – Hesham Mar 15 '19 at 19:31
  • it is project at my uni and it is important to apply this on text files. @Dortimer – Hesham Mar 15 '19 at 19:33
  • That's what my link goes to. – Dortimer Mar 15 '19 at 19:35
  • The link does not support how to read when the program starts and how to save when the program exit. Is this possible using constructor and destructor of the User class? @Dortimer – Hesham Mar 15 '19 at 19:39
  • If you scroll through it it provides examples on how to read data from a text file and how to write data to it. You just need to figure out where the best places to put the codes are for your purposes. It's entirely possible to add the code to the constructor and destructor. – Dortimer Mar 15 '19 at 19:46
  • @Dortimer i've tried your suggestion, but if 2 lists is been created the destructor is been called twice. i don't know why this happens. – Hesham Mar 15 '19 at 19:50
  • How many user objects are you working with? If you're using a list, it'll call the destructor for every object. If you only need one text file for multiple users, I'd suggest adding a separate class that handles the text file and that can handle the list of users. – Dortimer Mar 15 '19 at 19:54
  • i won't create multiple users from the class user, but class user will be inherited and there will be different types of users, so may be the destructor of the parent class been called for every user creation (different types). i hop i have explained my idea. @Dortimer – Hesham Mar 15 '19 at 19:59

2 Answers2

0

You probably want to write a little parser. Store the data in your textfile like this:

Username:username|Password:password

To make it easier to read the file. To read/write to the file use something like this:

static List<user> ReadUsers()
    {
        List<user> tmp_users = new List<user>();
        var Lines = File.ReadAllLines(AppDomain.CurrentDomain.BaseDirectory + "users.txt");
        foreach (var line in Lines)
        {
            user user = new user();
            String[] AccDetails = line.Split(new[] { "|" }, StringSplitOptions.None);
            var username = AccDetails[0].Split(new[] { ":" }, StringSplitOptions.None);
            var password = AccDetails[1].Split(new[] { ":" }, StringSplitOptions.None);
            user.Username = username[1];
            user.Password = password[1];
            tmp_users.Add(user);
        }
        return tmp_users;
    }
    static void WriteUsers()
    {
        using (StreamWriter writer = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "users.txt", false))
        {
            foreach (user user in users)
            {
                writer.WriteLine("Username:" + user.Username + "|Password:" + user.Password);
            }
        }
    }

EDIT: if you want to fire this when the fomr is opened/closed then create form.Load() / form.Close() events and put the code there. Edit 2: Github repo with a console application that does the user managerment: Repo

b3nj4m1n
  • 502
  • 7
  • 24
0

If you wait saving when program exits then you can pretty much loose all the data there. Instead you should synchronize and save every change to be sure about integrity. You can use this library or check their design https://github.com/rsevil/Transactions What you want to do is following transaction and again do it every time you add or delete never wait! If you fail and fail as soon as possible no other choice! pseudo code:

transaction block {
  create new users file in temp
  move users to somewhere where you can roll back
  move new file to current users file
}

I will warn you once as the time moves on you will start to think "why I am torturing myself why not I am using sqlite or something" just saying

Abdurrahim
  • 2,078
  • 1
  • 17
  • 23