无法将类型字符串隐式转换为StreamWriter

时间:2014-06-04 00:57:58

标签: c# string streamwriter

我正在尝试将文件拆分为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();
                }

1 个答案:

答案 0 :(得分:2)

要解决此问题,您必须实例化新对象StreamWriter

a.Close();
a = new StreamWriter(@"C:\work\missing" + num + ".txt");

但请记得先关闭它。