-1

I need to populate a list with parameters from a TXT file.

This is my Class:

 public  class Usuario

{
    private string id;
    private string pass;
    private string rol;

    public string Id { get { return id; } set { id = value; } }

    public string Pass { get { return pass; } set { pass = value; } }

    public string Rol { get { return rol; } set { rol = value; } }

    public Usuario(string id, string pass,string rol)
    {
        this.id = id;
        this.pass = pass;
        this.rol = rol;
    }

    public Usuario()
    {

    }



}

This is my method AddUser:

bool User AddUser(string id, string pass)
    {
        if (this.ListUsers == null) this.ListUsers = new List<User>();

        if (id != null && pass != null)
        {
            if (pass.Length >= 6 && pass.Any(char.IsLower) && pass.Any(char.IsDigit) && pass.Any(char.IsUpper))
            {

                    User user = new User()
                    {
                        Id = id,
                        Pass = pass,
                        Rol = "admin"

                    };

                    ListUsers.Add(user);
                    return true;
                }
                else { return false; }
            }
            else { return false; }
        }
        else { return false; }
    }

This is the TXT format i need to use: user#password#rol

admin1#Admin1234
admin9#Admin1234

How can i add Users from the TXT to the Users List by pressing a Button in the WebForm;

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86

1 Answers1

1

You could do this i guess, no error checking or fault tolerance though

var results = File.ReadAllLines("SomeFileName.Text")
                  .Select(x => x.Split('#'))
                  .Select(x => AddUser(x[0], x[1]))
                  .ToList();

The rest you can modify to your liking


Additional Resources

File.ReadAllLines

Opens a text file, reads all lines of the file into a string array, and then closes the file.

Enumerable.Select

Projects each element of a sequence into a new form.

String.Split

Returns a string array that contains the substrings in this instance that are delimited by elements of a specified string or Unicode character array.

TheGeneral
  • 79,002
  • 9
  • 103
  • 141