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.

5
  • Can you use class objects and List<class> 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. Commented Feb 29, 2020 at 16:17
  • Im limited with the CSV-format and i use the List<class>,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. Commented Feb 29, 2020 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. Commented Feb 29, 2020 at 16:32
  • Will do!Thanks for the heads up. Commented Feb 29, 2020 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. Commented Feb 29, 2020 at 17:08

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

How can i replace the row of the CSV-file?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.