我有一个 1000 000 条记录的文本文件,所以我想将文件分成许多文件,每个文件有 100 条记录。这是我使用 listbox1 来控制文件的代码。该代码正在运行,但丢失的记录较少。
private void WriteToFile()
{
int RowCount = listBox1.Items.Count;
string FileName = "C:\\Users\\bbdnet0986\\Documents\\MyExpotedQADATA";
StreamWriter sw = new StreamWriter(FileName + ".txt");
int inc = 0;
int counter = 0;
//StreamWriter sw = new StreamWriter(FileName+inc + ".txt");
for (int i = 0; i < listBox1.Items.Count; i++)
{
sw.WriteLine(listBox1.Items[i].ToString());
string me = listBox1.Items[i].ToString();
if (RowCount > 100)
{
listBox2.Items.Add(listBox1.Items[counter].ToString());
counter++;
if (counter == 100)
{
inc++;
sw = new StreamWriter(FileName + inc + ".txt");
RowCount = RowCount - counter;
counter = 0;
}
}
else
{
sw.WriteLine(listBox1.Items[i].ToString());
}
}
sw.Close();
}