Jump to content

Best way to store and read lists in c#

Vivitary

I'm looking for a way to store a list so that it can be recalled every time the program is used. currently my solution save the data to a .txt file but it cant read it and put it back in a list, any tips?

Link to comment
Share on other sites

Link to post
Share on other sites

Why can't you read it and put it back in a list...?

NEW PC build: Blank Heaven   minimalist white and black PC     Old S340 build log "White Heaven"        The "LIGHTCANON" flashlight build log        Project AntiRoll (prototype)        Custom speaker project

Spoiler

Ryzen 3950X | AMD Vega Frontier Edition | ASUS X570 Pro WS | Corsair Vengeance LPX 64GB | NZXT H500 | Seasonic Prime Fanless TX-700 | Custom loop | Coolermaster SK630 White | Logitech MX Master 2S | Samsung 980 Pro 1TB + 970 Pro 512GB | Samsung 58" 4k TV | Scarlett 2i4 | 2x AT2020

 

Link to comment
Share on other sites

Link to post
Share on other sites

Did you use the StreamReader?
invent your own file format more or less.

 

My data is seperated by semicolons.

Psuedocode goes something like this

 

foreach item in list

  streamwrite current index to text file

  streamwrite seperation indicator to text file. (new line, space, semicolon, something distinct)

 

Read files

read next character

if (character is seperation indicator)

  Go to next index

else

  Add value to current index

 

My GitHub:
https://github.com/hislittlecuzin/ReadFiles

Link to comment
Share on other sites

Link to post
Share on other sites

Install json.net from nuget. Create a serializable class that will be your data (one class that has the fields, and a second that holds a list of the field classes). Then serialize your data into the file (or create the json manually) and save to your text file. Now when you read your text file contents you can deserialize into your objects and use the data in your program. This way your code is effectively open file, dump contents into my object, use object. Very simple looking code. 

 

 

Json is very efficient, and this method can easily be upgraded in the future with a new storage technique like a database, with you only changing the code that retrieves the data and nothing else. 

Link to comment
Share on other sites

Link to post
Share on other sites

17 hours ago, Vivitary said:

I'm looking for a way to store a list so that it can be recalled every time the program is used. currently my solution save the data to a .txt file but it cant read it and put it back in a list, any tips?

Have you heard of serialization?

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×