I have data which I need to break and write into a .CSV file so it is a dummy code it is not writing full data in the file I contain thousand data and suppose if I take max 400 rows in a file it writes till 280 then start writing in another file from 401 Also I tried with 200 so it writes till 170 and started writing another file from 201 so my data is missing and my last file is coming blank
This is my code - please help:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TestProject
{
class Program
{
static void Main(string[] args)
{
int totalrows = 1000; //Total data to write
int maxrows = int.Parse(Console.ReadLine()); //Maximum rows to write in a file
int maxfile = 0; //Maximum file needed
int selectedrow=0; //Select row till which we need to write in single file
int executedrow = 0; //Record till that its already executed
if(maxrows!=0) //Calculate Maximum files needed to write complete data
maxfile = totalrows / maxrows;
string data = "";
//Folder to store file
string folder = @"C:\Users\Himanshu Goyal\Documents\WriteInFile\";
string filename, filepath;
//Write in File Object
for (int i = 0; i <= maxfile; i++)
{
selectedrow += maxrows;
if (selectedrow > totalrows)
selectedrow = totalrows;
//Create csv file
filename = DateTime.UtcNow.ToString("ddMMyyyyhhmm") +i+"of"+maxfile+".csv";
//Full File Path
filepath = folder + filename;
StreamWriter writer = new StreamWriter(filepath);
writer.WriteLine("Numbers" + "\n");
for (int j = executedrow; j <= selectedrow; j++)
{
data += j + "\n";
// Console.WriteLine(executedrow);
executedrow++;
}
writer.WriteLine(data);
Console.WriteLine(data);
data = "";
}
}
}
}