'Username & password are added in the same column of the other saved user

I have this code to save some csv data:

if (textBox2.Text ==textBox3.Text)
{
    string username = textBox1.Text;
    string password = textBox2.Text;
    string Fullname = username + "," + password;
               
                
    File.AppendAllText("Credentials.csv", Fullname);
                

    DialogResult r = MessageBox.Show("Account Created ", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
    textBox1.Clear();
    textBox2.Clear();
    this.Hide();
    Form8 f = new Form8();
    f.Show();
}

After running this code a few times, I expect a file with 3 rows and 2 columns as below:

ori,babmdt
sami,123
yara,456

Whereas the file I created looks like this screenshot

enter image description here

How can I fix this?



Solution 1:[1]

It's missing an end of line character at the end of each record. Change this:

string Fullname = username + "," + password;

to this:

string Fullname = $"{username},{password}\n";

But this is a profoundly unsafe way to handle passwords, and handling passwords is one of those things that's too important to do wrong, even for practice/learning projects. Go do some research on better patterns in this area before continuing.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Joel Coehoorn