C#:如何停止循环访问 csv 文件中的数据并存储最后读取的值

标签 c#

我有一个 Windows 窗体应用程序,它获取一个 csv 文件并循环访问其中的客户 ID。对于每个客户 ID,它都会进行更新。

我有两个问题:

  1. 我有一个暂停按钮,它应该停止处理 csv 文件中的 customerID,并存储最后处理的 customerID。如何停止遍历 csv 中的客户 ID 并存储最后处理的客户 ID?

  2. 下次我启动应用程序时,它应该在上次处理的客户 ID 之后开始处理

请问我该怎么做?

//initially the customerID will be 0
int customerID = 0;
string inputFilePath = "C:\\customer_refno\\customerids.csv";
using (StreamReader input = File.OpenText(inputFilePath))
using (CsvReader csvread = new CsvReader(input))
{
    IEnumerable<dynamic> records = csvread.GetRecords<dynamic>();
    //StringBuilder buildtext = new StringBuilder();
    foreach (var record in records)
    {
        //process customerID
        lastProcessed = record.CustomerID;

    }
}

private void btpause_click(object sender, EventArgs e)
{
    //complete processing the currently processing customerID and stop
    //store last processed customer ID 
}

最佳答案

您可以通过存储下划线 Stream 的位置来实现。但请注意缓冲区,可能需要跳过文件同一 block 中的某些缓冲区。

这里是一个示例:

    private static long _lastPos = 0;//Store this in a file and load on Apllication start
    private static int _lastProcessed = -1;//Store this in a file and load on Apllication start
    private static int _bufferSize = 4096;
    private static bool _break = true; //Set or reset in btpause_click


    static void Main(string[] args)
    {
        Loop();
        Loop();
        Loop();


    }

    private static void Loop()
    {
        int customerID = 0;
        string inputFilePath = "D:\\temp\\customerids.csv";
        using (StreamReader input = new StreamReader(inputFilePath, Encoding.UTF8, true, _bufferSize))
        {
            if (_lastPos > 0) input.BaseStream.Position = _lastPos;
            using (CsvReader csvread = new CsvReader(input, new Configuration(){Delimiter = "\t"}))
            {
                IEnumerable<dynamic> records = csvread.GetRecords<dynamic>();
                //StringBuilder buildtext = new StringBuilder();
                foreach (var record in records)
                {
                    //process customerID
                    var cosId = Int32.Parse(record.CustomerID);
                    if (_lastProcessed >= cosId) continue; //Skip to next if lower than processed
                    _lastProcessed = cosId;

                    _lastPos = input.BaseStream.Position - _bufferSize;
                    Console.WriteLine(" > " + record.CustomerID + "\t" + record.Name);
                    if (_break) {
                       //Do staff needed at the break, like save variables
                       break;
                    }
                }
            }
        }
    }

-- 编辑 1 -- 您可以通过将 _break 设置为 true 来停止循环(通常在示例中它应该为 false 它是真实的,以显示如何只获得一个客户)如果您需要获得反馈:

private void btpause_click(object sender, EventArgs e)
{
    _break = true;
}

// ...
private static void Loop()
  //... Modify loop, also see first sample in the case break is set:
  Console.WriteLine(" > " + record.CustomerID + "\t" + record.Name);
  if (_break) {
      //Do staff needed at the break, like save variables
      break;
  }
  //....

关于C#:如何停止循环访问 csv 文件中的数据并存储最后读取的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53830845/

相关文章:

c# - 如何同时在控制台应用程序中的多个位置写入? C#

c# - 如何让 Razor 读取没有 BOM 的 UTF-8 文件?

c# - 轨道力学

c# - 类方法 : should I always check variables before accessing them?

c# - 获取C#中声音设备的名称

c# - System.Data.Spatial DbGeography.Distance 单位?

c# - 不安全的 C# 和 2D 渲染指针,好还是坏?

c# - 存储库模式 - 切换出数据库并切换到 XML 文件

c# - 使用 linq 对列表进行采样

c# - WPF ListView 将 SelectedItem 与相同的项目混淆