'split a txt file into multiple files with the number of lines in each file being able to be set by a user

I'm writing something in C# and I need to find a way to split a text file into more files with the number of lines in the file being equivalent to a user input.

Example : file a had 1000 lines in it and I want the code to ask the user for a number and then use that number to make more files like this a = 1000 lines .

Then after the code has run with the input of 300

a = 300 lines

b = 300 lines

c = 300 lines

d = 300 lines

e = 300 lines

Repeat that until the original file has been split into more files all with 300 lines .

This is what I have so far

            var file = File.ReadAllLines(ofd.FileName);

            Console.Write("> ");
            int userlinestosplit = int.Parse(Console.ReadLine());

            ArrayList fileA = new ArrayList();
            for (int i = 0; i < userlinestosplit; i++)
            {
                string line = file[i];
                fileA.Add(line);
            }
            int linesleft = file.Length - userlinestosplit;

            ArrayList fileB = new ArrayList();
            for (int i = linesleft; i < file.Length; i++)
            {
                string line = file[i];
                fileB.Add(line);
            }

            string[] fileAArr = (string[])fileA.ToArray(typeof(string));
            string[] fileBArr = (string[])fileB.ToArray(typeof(string));

            string resdir = "results";
            string modir = "splited";

            Directory.CreateDirectory(resdir);
            Directory.SetCurrentDirectory(resdir);

            Directory.CreateDirectory(modir);
            Directory.SetCurrentDirectory(modir);

            File.WriteAllLines("FA.txt", fileAArr);
            File.WriteAllLines("FB.txt", fileBArr);
            Console.ReadKey();

Any help would be greatly appreciated



Solution 1:[1]

Splitting text file to multiple parts

        public void SplitFile(string inputFile, int size, string path)
        {
            int index = 0;
            string s = string.Empty;
            using (StreamReader sr = File.OpenText(inputFile))
            {
                while (true)
                {
                    if (sr.EndOfStream) break;
                    using (StreamWriter output = new StreamWriter($"{path}\\part{index}.txt", false, Encoding.UTF8))
                    {
                        int linesRead = 0;
                        while ((s = sr.ReadLine()) != null && linesRead < size)
                        {
                            output.WriteLine(s);
                            linesRead++;
                        }                        
                    }
                    index++;
                }
            }
        }

How to use:

  
var inputFile = "test.txt";
int size =300;
SplitFile(inputFile, size, "c:\\data");

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 M.Hassan