0

I have created a couple of forms which all must work with a csv file. I can register different users and then login in a profile if it exists. I add the information with StreamWriter:

using (StreamWriter writer = new StreamWriter(filePath, true))
      {
           writer.WriteLine(username + "," + password);
      }

Then i read the information with:

using (StreamReader reader = new StreamReader(filePath))
            {
                while (!reader.EndOfStream)
                {
                    string line = reader.ReadLine();
                    string[] userData = line.Split(',');

                    usernames.Add(userData[0]);
                    passwords.Add(userData[1]);
                }
            }

And

 bool isFound = false;
            for (int i = 0; i < usernames.Count; i++)
            {
                if (usernames[i] == username && passwords[i] == password)
                {
                    isFound = true;
                }
            }

I want to add other information to the line like picture location,amount of money,amount of garage space,but everything i've tried from the internet writes the data on a new line,not to the existing line. I got a few tips from our lector-to first read the file and then write to it,but it still doesn't work. Whenever i add the data i use

    using(StreamWriter writer = new StreamWriter(filePath,true))
{
     writer.Write("the username from the register from" + "," + "the password from the register form" + openFileDialog.FileName("the location of the file/What my lector told me to use"));
}

I only need to know how to add that additional data so i can proceed with the rest of my project. Im sorry if this question has already been answered and thank you for your time.

  • Can you use class objects and `List` collections? Do you know how to override `[object].ToString()`? Can you use other forms of serialization or you're stuck with a CSV-like format? Btw, as you write to a file: `writer.WriteLine(username + "," + password);`, you can add more *fields* and write a line the same way. – Jimi Feb 29 '20 at 16:17
  • Im limited with the CSV-format and i use the List,i even add the opeFileDialog.FileName to a List<>.I've tried a couple of different things,one was the above,with another i get an Augment Out of Range exception.The problem is that the information they explained to me was little. – Elin Nikolov Feb 29 '20 at 16:23
  • You know that, if you want to add something like an *amount of money* and you use a comma as decimal separator, you cannot split the string using the comma as the Fields separator? In other words, you cannot use a comma anywhere, unless you enclose a Field in quotes or similar delimiters. So, maybe also consider the separator you're using. You can use another one, like a pipe `|`. Or be prepared to also parse quoted fields. – Jimi Feb 29 '20 at 16:32
  • Will do!Thanks for the heads up. – Elin Nikolov Feb 29 '20 at 16:37
  • Whenever i add more to the "writer.WriteLine(username "," password ", ")",then i have to separate it with "string[] userData = line.Split(","); i get the index out of range/bounds. – Elin Nikolov Feb 29 '20 at 17:08

1 Answers1

0

Can´t you use replace to add this information? If you have the username and password, just replace it to the same value and plus other information

  • How can i replace the row of the CSV-file? – Elin Nikolov Mar 02 '20 at 19:33
  • Some samples: https://stackoverflow.com/questions/13509532/how-to-find-and-replace-text-in-a-file-with-c-sharp/13509665 https://stackoverflow.com/questions/1915632/open-a-file-and-replace-strings-in-c-sharp/1915689 – Marcelo Monteiro Mar 04 '20 at 20:39