Tjeerd Bakker
Tjeerd Bakker

Reputation: 51

C# easiest way of storing strings to external file

C# TL;DR: I want the user to be able to input text, which is then written to an external file, which then can be called later based on position in the file. What's the easiest way and form to read and write a list of strings to?

Very amateur question, but I can't seem to find an easy anwer on the internet. If I'm missing something obvious, please redirect me. I am writing a very simple program in which the user can input a string which is then written (added) to an external file, from which later a string can be called based on the position in the file. I found things like JSON, Resource file, SQL DB... Problem is that due to my lack of programming experience I have no idea what's the best option to look into.

Example of what I want to achieve:

User inputs strings 'Dog', 'Cat', and 'Horse' into the textbox. Each of these strings are added to the external file. Lateron, the user calls the 2nd number on the list, to which the program returns 'Cat'.

Thanks!

Upvotes: 0

Views: 821

Answers (1)

ADeveloper
ADeveloper

Reputation: 58

If you already know the kind of data that will be saved I recommend using XML Serialization. This lets you save and read your file very easily. The linked example is from Microsoft and shows a dataset being serialized. If you want to save a generic list instead of a fixed object you might find this link helpful.

Alternatively, you could save data to your application configuration file (search online for "C# application configuration for PROJECT_TYPE" where the project type is winforms/mvc/class library etc..)

Upvotes: 1

Related Questions