简体   繁体   中英

Cannot implicitly convert type string to to StreamWriter

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();
                }

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM