Posting my solution here incase anyone else runs into the same problems.
I have a list of objects with people, which have a name, an address and an age.
In the class I have functions to get those.
Code
public void writePeople()
{
using (FileStream fileStream = new FileStream("people.txt", FileMode.OpenOrCreate, FileAccess.Write))
{
using (StreamWriter writer = new StreamWriter(fileStream))
{
foreach (var Person in People)
{
writer.WriteLine(Person.getName + ":" + Person.getAddress + ":" + Person.getAge);
}
writer.Flush();
}
}
}
I write them all seperated by a ":" (you can use whatever you want)
Then I read them back by cutting off the string everytime it sees ":"
and assigning them to their variables so I can add them to the list again.
Code
public void readPeople()
{
People.Clear();
using (FileStream fileStream = new FileStream("people.txt", FileMode.OrCreate, FileAccess.Read))
{
using (StreamReader reader = new StreamReader(fileStream))
{
string line;
while ((line = reader.ReadLine()) != null)
{
string[] propArray = line.Split(':');
string name = propArray[0];
string address = propArray[1];
int age = int.Parse(propArray[2]);
// Add the person with these things.
People.Add(
new Person(name, age, address)
);
}
}
}
}
you have to use filemode.create so it makes a new file, else it might keep the old list and not write a new one.
(atleast if you want to write a new list everytime).
Hope anyone is helped by this ✌
Thanks for the people that tried to help me, and goodluck to those who need it!