Home > Back-end >  Username & password are added in the same column of the other saved user
Username & password are added in the same column of the other saved user

Time:04-19

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?

CodePudding user response:

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.

CodePudding user response:

  1. You are just adding text to a CSV file -> Excel displays as you have added.
  2. @Igor made a good suggestion
  3. Why are you trying to store user/pass in plaintext?
  4. Please name your variables in a meaningful way
  •  Tags:  
  • c#
  • Related