Split large file into smaller files by number of lines in C#?

前端 未结 3 1216
一向
一向 2021-02-06 18:58

I am trying to figure out how to split a file by the number of lines in each file. THe files are csv and I can\'t do it by bytes. I need to do it by lines. 20k seems to be a goo

3条回答
  •  逝去的感伤
    2021-02-06 19:32

    int index=0;
    var groups = from line in File.ReadLines("myfile.csv")
                 group line by index++/20000 into g
                 select g.AsEnumerable();
    int file=0;
    foreach (var group in groups)
            File.WriteAllLines((file++).ToString(), group.ToArray());
    

提交回复
热议问题