I am writing into a .csv file from multiple dictionaries. I want to limit the max size of this output file by 10,000 rows and create new files to print next lines.
The following code is writing to the file OutputFile. If say, the output file has 60,000 rows, then instead of printing 60,000 rows in a single file, I want 10,000 rows to be printed in one file and rest of the lines in other files. So, I Will have 6 outputFiles with 10K rows in each file.
using (StreamWriter writer = new StreamWriter(OutputPath + "OutputFile-" + DateTime.Now.ToString("MM.dd.yy_hh.mm.ss.tt") + ".csv"))
{
foreach (var program in _Code1)
{
string Id = program.ToString();
foreach (var entry in _List1)
{
if (!string.IsNullOrEmpty(entry.Value.ToString().Trim()))
{
string LineValue = entry.Value.ToString();
string[] SplitedLine = LineValue.Split(',');
string query = "\"Insert into table(Id,UserID) values('" + Id + "','" + SplitedLine[1] + "')\"";
writer.WriteLine(query);
}
}
}
foreach (var program in _Code2)
{
string Id = program.ToString();
foreach (var entry in _List2)
{
if (!string.IsNullOrEmpty(entry.Value.ToString().Trim()))
{
string LineValue = entry.Value.ToString();
string[] SplitedLine = LineValue.Split(',');
string query = "\"Insert into table(Id,UserID) values('" + Id + "','" + SplitedLine[2] + "')\"";
writer.WriteLine(query);
}
}
}
foreach (var program in _Code1)
{
string Id = program.ToString();
foreach (var entry in _List3)
{
if (!string.IsNullOrEmpty(entry.Value.ToString().Trim()))
{
string LineValue = entry.Value.ToString();
string[] SplitedLine = LineValue.Split(',');
string query = "Delete From table Where ID='" + Id + "' And UserID='" + SplitedLine[1] + "'";
writer.WriteLine(query);
}
}
}
}
I have looked into this Split large file into smaller files by number of lines in C#? But the solution here is writing from one file so it can count the number of rows but in my case I am writing from multiple dictionaries so how will I keep a tab of the max limit of the output file ?