How to write a c# console application to split text files into multiple files with number of rows indicated by user

I have written a few lines of code to create a c# console application to split a text file into multiple files using the input from the user. This divides the file into lines of 20000 each. I want a user to be able to choose.

I want it to prompt a question “How many lines per file would you like to have?”

For a scenario in which a user has a text file with 200000 lines and wants 1000 lines per file. It will divide the text file into multiples with 1000 lines each. Also another user can want 500 lines per file.

my code looks like this

string fileName = @"C:\Users\COURE-TECH\source\repos\ConsoleApp1\ConsoleApp1\dnd.txt";

            var fileSuffix = 0;
            int lines = 0;
            Stream fstream = File.OpenWrite($"{fileName}" + (++fileSuffix) + ".txt");
            StreamWriter sw = new StreamWriter(fstream);

            using (var file = File.OpenRead(fileName))
            using (var reader = new StreamReader(file))
            {
                    
                    while (!reader.EndOfStream)
                {
                    
                    while (true)
                    {
                        Console.WriteLine("How many lines per file would you like to have?");
                        if (int.TryParse(Console.ReadLine(), out lines))
                        {
                            break;
                        }
                        Console.WriteLine("Please enter an integer value!");
                    }

                    sw.WriteLine(reader.ReadLine());
                    lines++;                                
                        sw.Close();
                        fstream.Close();
                        lines = 0;
                        fstream = File.OpenWrite($"{fileName}"+ (++fileSuffix) + ".txt");
                        sw = new StreamWriter(fstream);                  
                }
}

            sw.Close();
            fstream.Close();
            Console.WriteLine("Done");

I expect a prompt question “How many lines per file would you like to have?”

And divide the file into the lines the user wants

Why doesn’t it work? I think I have an understanding of what you are trying to do, but have you identified where it is breaking?

I have done TryParse to force user to give a valid existent integer. Including with a while loop. But it just splits into a single file with only the first line now. Instead of splitting into multiple txt files. What is the cause of that and how do I rectify it.