Reputation: 169
I'm trying to split a file in to 10 pieces. This is the way I think I can do it but I'm getting the error mentioned in the title. Is there an easy fix to this problem?
StreamWriter a = new StreamWriter(new FileStream(@"C:\work\missing" + num + ".txt");
using (var r = new StreamReader(readMissing))
{
var rr = r.ReadLine();
while (rr != null)
{
a.WriteLine(rr);
count++;
if (count == 36139)
{
iNum = Int32.Parse(num);
iNum++;
num = iNum.ToString();
a = (@"C:\work\missing" + num + ".txt"); //problem line
count = 0;
}
rr = r.ReadLine();
}
a.Close();
}
Upvotes: 0
Views: 373
Reputation: 3915
To fix the problem you have to instantiate a new object StreamWriter
:
a.Close();
a = new StreamWriter(@"C:\work\missing" + num + ".txt");
but remember to close it first.
Upvotes: 2