Mitchell
Mitchell

Reputation: 169

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

Upvotes: 0

Views: 373

Answers (1)

Saverio Terracciano
Saverio Terracciano

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

Related Questions