End Google Ads 201810 - BS.net 01 --> Please bare with me I only started learning c# a few days back.
Anyway I am creating a split function (by line not file size) and thus far I have got it to create do the necessary math & create its directory. However I have a problem in my loop in which the error: Cannot write to a closed TextWriter. I just cant seem to solve this problem and need some help!


private void btnSplit_Click(object sender, EventArgs e)
{
string inputFile = this.txtFileToImport.Text; // Substitute this with your Input File
FileStream fs = new FileStream(inputFile, FileMode.Open, FileAccess.Read);
int numberOfFiles = (int)this.numericUpDown1.Value;
int numberOfRows = (int)this.dataGridView_preView.RowCount - 1;
decimal rowsPerFile = numberOfRows / numberOfFiles;
int sizeOfEachFile = (int)Math.Ceiling((double)rowsPerFile);
lblPray.Text = " Rows: " + sizeOfEachFile.ToString();

string OutputFolder = inputFile + "_Batches";
if (Directory.Exists(inputFile) == false)

{
Directory.CreateDirectory(OutputFolder);
}

// Read the csv column's header
System.IO.StreamReader reader = new System.IO.StreamReader(inputFile);
string strHeader = reader.ReadLine();

// Start splitting
int FileIndex = 0;

do
{

// Create new file to store a piece of the csv file
string PiecePath = OutputFolder + "\\" + Path.GetFileNameWithoutExtension(inputFile) + "_" + FileIndex + Path.GetExtension(inputFile);
StreamWriter Writer = new StreamWriter(PiecePath, false);
Writer.AutoFlush = false;
Writer.WriteLine(strHeader);

// Read and writes precise number of rows
for (int i = 1; i