简体   繁体   English

C# - 将数据保存到.txt文件,编辑删除

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

I use terminal to add information that is saved to csv file我使用终端添加保存到 csv 文件的信息

        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.我已将所有必需的信息保存到我的 csv 文件中。 How do I edit or delete it from the csv file it is saved on, using terminal?如何使用终端从保存它的 csv 文件中编辑或删除它?

So for example I saved this data:例如,我保存了这些数据:

Date - 12.03, Time - 12, Name - John, Description - Went to bed, Number - 3,日期 - 12.03,时间 - 12,姓名 - 约翰,描述 - 上床睡觉,号码 - 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,日期 - 12.03,时间 - 11,姓名 - 约翰,描述 - 上床睡觉,号码 - 5,

A CSV file doesn't really lend itself to updates in the way you want. CSV 文件并不能真正以您想要的方式进行更新。 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.但是,看起来有 3rd 方库可以帮助您做您想做的事情。

https://www.codeproject.com/Articles/25133/LINQ-to-CSV-library 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.如果您不想使用第 3 方库,那么我认为您必须重新创建 CSV 文件才能更新该行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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