![]() |
|
[C#] Saving a list in textfile - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [C#] Saving a list in textfile (/showthread.php?tid=5849017) |
Saving a list in textfile - Tunabish - Jun 13, 2018 I need to save a list in a textfile and load the lists back from the textfiles but I have no idea how I was told to use JSON but I don't think I'm allowed to as my uni requires it to be a textfile for some stupid reason. Does anyone know how to do this? x_x RE: Saving a list in textfile - HFDone - Jun 13, 2018 foreach (var item in listBox1.Items) { File.AppendAllText("text.txt", item); } string[] lines = File.ReadAllLines("text.txt"); listBox1.Items.AddRange(lines); RE: Saving a list in textfile - Tunabish - Jun 13, 2018 (Jun 13, 2018 10:49 AM)HFDone Wrote: foreach (var item in listBox1.Items) { A list of objects which I need to load back on startup, not a listbox. RE: Saving a list in textfile - Nick (Name) - Jun 13, 2018 This is my stab at what I believe you're trying to accomplish. It's written in .NET 4.5 and uses async tasks. Source: Spoiler (Click to View) RE: Saving a list in textfile - Cr1p - Jun 14, 2018 Bruh. If your objects are serializable into strings (which I assume they are because you think you can write them to a text file), use Newtonsoft.Json var myList = new List<MyObject>(); var myString = JsonConvert.SerializeObject(myList); File.WriteAllText(myString......); var x= File.ReadAllText(...); var myList = JsonConvert.DeserializeObject<List<MyObject>>(x); Just realized you said not to use JSON. In that case, there really isn’t much we can provide for you unless you give us the object type you want to write to a file. RE: Saving a list in textfile - Moxxi - Jun 14, 2018 assuming list of strings and console app, This should work for what you want options are save, load, display, and exit Spoiler (Click to View) RE: Saving a list in textfile - 0xDEAD10CC - Jun 14, 2018 (Jun 13, 2018 10:49 AM)HFDone Wrote: foreach (var item in listBox1.Items) { Or, you could use WriteAllLines, since appending text without a line isn't going to work unless each listbox object to string contains it's own newline at the end, and the next time you write to the file it won't clear the first list. OP: And if it is a list of objects, why would you write them out line by line? Just serialize the information and deserialize it? RE: Saving a list in textfile - Tunabish - Jun 16, 2018 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()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()(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! RE: Saving a list in textfile - CodeAssassin - Jun 16, 2018 Would make sense to override the ToString() method and set your delimited string there Example Code public override string ToString() {RE: Saving a list in textfile - 0xDEAD10CC - Jun 16, 2018 (Jun 16, 2018 05:13 AM)CodeAssassin Wrote: Would make sense to override the ToString() method and set your delimited string there Not really, because the ToString() method by convention isn't typically used for parsing, it's normally used for displaying an object in human readable form. I would separate it into another method to make it clear that the string output would be the format required for the parsing if this is the way he's choosing to go over serialization.. |