Engin
Engin

Reputation: 9

StreamReader failing to read second line

I am using a StreamReader to read out the Username and Password from a .csv file (I'm just trying this out because I'm new to C#). For some reason, if I press sign in it works for the account in the first line of the file but not the second one. Anyone can help?

Register Code (Writer):

try
{
    using (StreamWriter sw = new StreamWriter("C:\\GameLauncherKarakurt\\users.csv", true))
    {
        sw.WriteLine(tbxUsername.Text + ';' + tbxPassword.Text);
        sw.Close();
    }
}
catch
{
    // Do nothing
}

Sign-in Code (Reader):

string username;
string password;

bool usernameCorrect = false;
bool passwordCorrect = false;

// Username
try
{
    using (StreamReader sr = new StreamReader("C:\\GameLauncherKarakurt\\users.csv"))
    {
        username = sr.ReadLine();

        string[] usernameSplit = username.Split(';');
        string user = usernameSplit[0];

        while (username != null)
        {
            if (tbxUsername.Text == user)
            {
                usernameCorrect = true;

                break;
            }
            else
            {
                username = sr.ReadLine();
            }
        }
        sr.Close();
    }
}
catch
{
}

// Password
try
{
    using (StreamReader sr = new StreamReader("C:\\GameLauncherKarakurt\\users.csv"))
    {
        password = sr.ReadLine();

        string[] passwordSplit = password.Split(';');
        string pass = passwordSplit[1];

        while (password != null)
        {
            if (tbxPassword.Text == pass)
            {
                passwordCorrect = true;

                break;
            }
            else
            {
                password = sr.ReadLine();
            }
        }
        sr.Close();
    }
}
catch
{
}

if (usernameCorrect == true && passwordCorrect == true)
{
    pnlMenu.Visible = true;

    tbxUsername.Clear();
    tbxPassword.Clear();
}

Upvotes: 0

Views: 365

Answers (2)

Tiago Vaccari
Tiago Vaccari

Reputation: 1296

Once your file has been created, you can read the user and password from each line using the statement below:

using (StreamReader sr = new StreamReader(""C:\\GameLauncherKarakurt\\users.csv""))
{
    while (!sr.EndOfStream)
    {
        strLine = sr.ReadLine();
        string[] usernameSplit = strLine.Split(';');
        string user = usernameSplit[0];
        string password = usernameSplit[1];
    }
}

Upvotes: 0

Jonathan Applebaum
Jonathan Applebaum

Reputation: 5986

You are not keeping the structure of CSV file.
all couples of user and password must be separated by comma (",") and not by a new line,
Add a comma after each user and password couple.
Use Write() and not WriteLine().

private void Write()
{
    try
    {
        using (StreamWriter sw = new StreamWriter("C:\\GameLauncherKarakurt\\users.csv", true))
        {
            // next time you add user and pass word you need to concatenate with ","
            sw.Write(tbxUsername.Text + ';' + tbxPassword.Text + ",");           
            sw.Close();
        }
    }
    catch()
    {
        // Do nothing
    }
}

Here is one way to read the file.
Use ReadToEnd() and split all the user and password couples to an array, each couple will be at a different index:

using (StreamReader sr = new StreamReader("C:\\GameLauncherKarakurt\\users.csv"))
{
    var rows = sr.ReadToEnd();
    string[] splitted = rows.Split(',');

}

Now you can split again each index of the array by the ";" separator,
keep in mind that there are some improvements you can do to your "matching"/authentication logic.

Upvotes: 2

Related Questions