public class Program { public static void Main() { var path = @"C:\Users\tlamb\Downloads\AccessGUDID_Delimited_Full_Release_20231001"; var input = "gmdnTerms.txt"; var format = "gmdnTerms-{0:000}.txt"; //var input = "device.txt"; //var format = "device-{0:000}.txt"; var size = int.MaxValue; var info = new FileInfo(Path.Combine(path, input)); size = (int)Math.Min(info.Length / 10, size); Split(Path.Combine(path, input), Path.Combine(path, format), size); } public static void Split(string inputfile, string outputfilesformat, int maxSize = int.MaxValue, bool hasHeader = true) { int i = 0; int size = 0; StreamWriter outfile = null; bool firstFile = true; string headerLine = null; string line; try { using (var infile = new StreamReader(inputfile)) { while (!infile.EndOfStream) { line = infile.ReadLine(); if (hasHeader && headerLine == null) { headerLine = line; } if (size > maxSize) { if (outfile != null) { outfile.Dispose(); outfile = null; size = 0; } } if (string.IsNullOrEmpty(line)) { if (outfile != null) { outfile.Dispose(); outfile = null; size = 0; } continue; } if (outfile == null) { outfile = new StreamWriter( string.Format(outputfilesformat, i++), false, infile.CurrentEncoding); if (!firstFile && hasHeader) { outfile.WriteLine(headerLine); } firstFile = false; } outfile.WriteLine(line); size += line.Length; } } } finally { if (outfile != null) outfile.Dispose(); } } }