简体   繁体   中英

C# - save data to .txt file, edit and delete

I use terminal to add information that is saved to csv file

        string path = @"Savedata.csv";
        using (StreamWriter sw = new StreamWriter(path, true))
        {
            List<Diary> parts = new List<Diary>();

            parts.Add(new Diary
            {
                Date = Convert.ToInt32(Console.ReadLine()),
                Time = Convert.ToInt32(Console.ReadLine()),
                Name = Console.ReadLine(),
                Description = Console.ReadLine(),
                Number = Convert.ToInt32(Console.ReadLine())
            });

           //public struct Diary

I have got all the required information saved to my csv file. How do I edit or delete it from the csv file it is saved on, using terminal?

So for example I saved this data:

Date - 12.03, Time - 12, Name - John, Description - Went to bed, Number - 3,

I now want to edit it so it looks like this:

Date - 12.03, Time - 11, Name - John, Description - Went to bed, Number - 5,

A CSV file doesn't really lend itself to updates in the way you want. A database would be better for your purpose. However it looks like there are 3rd party libraries which will help you do what you want to do.

https://www.codeproject.com/Articles/25133/LINQ-to-CSV-library

If you don't want to use a 3rd party library then I think you will have to recreate the CSV file in order to update that one line.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM