使用 C# 在 csv 文件中写入更改

write changes in a csv file with c#

我正在尝试在 C# 控制台中更改 csv 文件的值。 首先,我将 csv 文件的内容加载到代码中。然后我更改一个值并尝试保存它。

这是执行前(遗憾的是执行后)csv 的内容:

1;Geg;17
2;Geg;17
3;Geg;17

在运行期间,值变为: new Values

但不知何故,它们没有保存到 csv 文件中。

这是我的代码:

主要class:

     using System;
     using System.Collections.Generic;
     using System.Data;
     using System.IO;
     using System.Text.RegularExpressions;
     
     namespace csvtest
     {
         class Program
         {
             static void Main(string[] args)
             {
                 Tabelle tab = new Tabelle();
                 foreach (Employees e in tab.getAll())
                 {   
                     Console.WriteLine(e);
                 }
     
                 tab.updating(1, "fafdsdsf", 323);
     
             }
             
         }
     }

Tabelle class:

     using System;
     using System.Collections.Generic;
     using System.Linq;
     using System.Text;
     using System.Threading.Tasks;
     using System.IO;
     
     namespace csvtest
     {
         class Tabelle
         {
             private List<Employees>liste;
             public Tabelle()
             {
                 
                 liste = new List<Employees>();
                 string[] rows = File.ReadAllLines(@"C:\Users\rbc\Desktop\csvtest\csvtest\obj\test.csv");
                 foreach (string row in rows)
                 {
                     string[] information = row.Split(';');
                     int number = int.Parse(information[0]);
                     string name = information[1];
                     double salary = double.Parse(information[2]);
     
                     liste.Add(new Employees { Number = number, Name = name, Salary = salary });
                 }
             }
             public  Employees[] getAll()
             {
                 return liste.ToArray();
                 
             }
             public void adding(int number, string name, double salary)
             {
                 liste.Add(new Employees { Number = number, Name = name, Salary = salary });
             }
             public void updating(int number, string name, double salary)
             {
                 foreach (Employees e in liste)
                 {
                     if (e.Number == number)
                     {
                         e.Name = name;
                         e.Salary = salary;
                     }
                 }
             }
             ~Tabelle()
             {
                 string[] information = new string[liste.Count];
                 for (int i = 0; i<liste.Count; i++)
                 {
                     information[i] = liste[i].Number + ";" + liste[i].Name + ";" + liste[i].Salary;
                 }
                 File.WriteAllLines(@"C:\Users\rbc\Desktop\csvtest\csvtest\obj\test.csv", information);
     
             }
         }
     }
     

员工class:

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 
 namespace csvtest
 {
     class Employees
     {
         public int Number { get; set; }
         public string Name { get; set; }
         public double Salary { get; set; }
 
 
         public override string ToString()
         {
             return Number + "|" + Name + "|" + Salary;
         }
     }
 }

我不知道问题出在哪里。我感谢任何想法或提示。

非常感谢您的帮助!!

正如其他人在这里建议的那样,开发人员确实无法控制何时调用终结器。 垃圾收集器 决定何时释放内存。

但是,你应该看看Dispose。如果您实现 IDisposable,并释放您的对象,那么 Dispose 中的代码将 运行.

首先,你的class应该实现上面的接口

class Tabelle : IDisposable

然后您需要使用 Dispose() 方法从析构函数中包装您的代码。

public void Dispose()
{
    string[] information = new string[liste.Count];
    for (int i = 0; i < liste.Count; i++)
    {
       information[i] = liste[i].Number + ";" + liste[i].Name + ";" + liste[i].Salary;
    }
    File.WriteAllLines(@"data.txt", information);
    GC.SuppressFinalize(this);
}

此外,在创建 class.

时不要忘记使用 using 关键字
 using (Tabelle tab = new Tabelle())
 {
       foreach (Employees e in tab.getAll())
       {
           Console.WriteLine(e);
       }

       tab.updating(1, "fafdsdsf", 323);
 }