0

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!

  • 6
    You can't find anything online about reading and writing strings to/from a file...? Did you find the documentation on the `File` class? https://msdn.microsoft.com/en-us/library/system.io.file(v=vs.110).aspx – Broots Waymb Aug 10 '18 at 13:56
  • 1
    `System.IO.File.WriteAllText("C:\path\to\your\file.txt", "your text here")` – jtate Aug 10 '18 at 13:58
  • I can't find/decide on the easiest way of storing strings to an external file. I should be able to figure out reading and writing once I know which method would work best. @BrootsWaymb – Tjeerd Bakker Aug 10 '18 at 13:59
  • There are plenty of simple ways to read/write files. You'll get dozens of quality hits by just googling something like "c# read/write file example" or something. I'm also slightly confused as to what you mean by "external" file. What is the file external in relation to? – Broots Waymb Aug 10 '18 at 14:01
  • 1
    What do you mean by *"work best"*? What's *"best"* usually depends on what your specific requirements are. And even when the requirements are known then this is still kind of subjective (since different people prefer different ways of working, use different libraries, prefer different code styles, etc)... – bassfader Aug 10 '18 at 14:03
  • The file should be external to the running program. I should be able to close the program, reopen it later and still be able to acces the stored data @BrootsWaymb . – Tjeerd Bakker Aug 10 '18 at 14:05
  • You should just try an approach and see how it works. Your title specifies *easiest* but your comment specifies *best*. These are not the same. Just try something. – Dan Wilson Aug 10 '18 at 14:05
  • The 'System.IO.File.WriteAllText' that was suggested will override the existing file everytime it is run (or so says microsoft on https://msdn.microsoft.com/en-us/library/system.io.file.writealltext(v=vs.110).aspx) @jtate – Tjeerd Bakker Aug 10 '18 at 14:05
  • @TjeerdBakker I was just providing the simplest way to write to a file. If you can provide more concrete requirements and what you're trying to accomplish, we could help you further. – jtate Aug 10 '18 at 14:07
  • @TjeerdBakker - That example from jtate is just one of many ways to write to a file. Again, you'll need to do a little research to find what method works best for your needs. Seriously, just look through the methods of the `File` class. It has some great stuff for beginners. – Broots Waymb Aug 10 '18 at 14:08
  • @bassfader with 'best' I mean 'The easiest way for a complete beginner to implement'. It doesn't have to be fast or efficient or able to handle billions of strings, it just needs to be easy to program. Sorry for the lack of clarification! – Tjeerd Bakker Aug 10 '18 at 14:08
  • Sorry for the lack of clarification everyone, I'm still not quite sure what the options are, which can be used for the purpose of storing a long list of strings, and which option is the most basic. – Tjeerd Bakker Aug 10 '18 at 14:09

1 Answers1

1

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..)

ADeveloper
  • 58
  • 7
  • 1
    If only strings are required, *and* if the OP knows that the strings won't contain line breaks, I'd suggest that XML or configuration files are overkill. `File.AppendAllLines` and `File.ReadAllLines` would be significantly simpler IMO. – Jon Skeet Aug 10 '18 at 14:19
  • Agreed with @DaisyShipton. If OP is unfamiliar with simple read/write, XML is definitely going to add unnecessary confusion. The solution would work, but OP is going to struggle much more for little (if any) gain. – Broots Waymb Aug 10 '18 at 14:44
  • @DaisyShipton This is exactly the answer I was looking for! Solely strings, no line breaks, easy navigation... Perfect! – Tjeerd Bakker Aug 10 '18 at 14:49