Having an issue with StreamWriter

I am using StreamWriter to write out a file that I'm reading in using File.ReadAllLines.  This file is a little over 2MB in size and my issue is that when I go to look at the file that StreamWriter has created, it is not complete.  I have stopped the code towards the end and parsed my way through and it is getting to the last lines (of the original file) and running normally through the code....ie. hitting the wr.Write(s.tostring());  

Is there something I am missing that won't allow StreamWriter to complete the entire file?  Do I need to increase the BufferSize when initializing the StreamWriter?  Any help would be appreciated.
StreamWriter wr = new StreamWriter("C:\\Test.inx", true);

            if (FileUploadCompleted(txtPrepressFile.Text))
                processed = true;
            try
            {
                if (processed)
                {                    
                    string[] AllContent = File.ReadAllLines(txtPrepressFile.Text);

                    foreach (string s in AllContent)
                    {
                        wr.WriteLine(s.ToString());
                    }
                }
            }
            catch (Exception f)
            {
                clsGlobal.ErrorLog(f.ToString());
            }

Open in new window

LVL 1
drunk_irishmanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Richard LeeSoftware EnthusiastCommented:
Try flushing and closing the streamwriter when you are done.

wr.Flush().
wr.Close();

or place the streamWriter within a using block

using(StreamWriter wr = new StreamWr....)
{
}

DaTribe
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
lazyberezovskyCommented:
Isn't it better to use
File.Copy(txtPrepressFile.Text, "C:\\Test.inx");
?
0
drunk_irishmanAuthor Commented:
Well, I dumbed down my code a little for this purpose.  There's more to it, some processing is going to take place but I needed to confirm that what I'm putting in, I am also getting out.
0
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

lazyberezovskyCommented:
Then, yes best way is dispose stream via 'using' statement.
0
Richard LeeSoftware EnthusiastCommented:
What is the result of closing the stream? The above example gave two ways to do this.

DaTribe
0
drunk_irishmanAuthor Commented:
So far, I have used the
wr.Flush();
wr.Close();

and that seemed to work on just taking what's coming in and pushing it right back out.  My first run of actually changing some data and verifying data, it did not work.  So I am now going to try the using block.
0
drunk_irishmanAuthor Commented:
It also take about 20 minutes for the code to run when I'm doing all the data processing.  I'll report results asap.
0
Richard LeeSoftware EnthusiastCommented:
If the data being written is not what is expected then double check that the updated data is what is being sent to the StreamWriter.

wr.Flush();
wr.Close();

or

wr.Close()

or

using(StreamWriter wr = new StreamWr....)
{
}

all do the same thing. No need to try that.

DaTribe
0
drunk_irishmanAuthor Commented:
Ok, the Flush and Close did take care of it.  

Thank you very much for your help, both of you!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.