我正在尝试将文件拆分为10个。这是我认为我可以做到的方式,但我收到了标题中提到的错误。是否可以轻松解决此问题?
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();
}
答案 0 :(得分:2)
要解决此问题,您必须实例化新对象StreamWriter
:
a.Close();
a = new StreamWriter(@"C:\work\missing" + num + ".txt");
但请记得先关闭它。