Link to home
Start Free TrialLog in
Avatar of drunk_irishman
drunk_irishman

asked on

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

ASKER CERTIFIED SOLUTION
Avatar of Avodah
Avodah
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Isn't it better to use
File.Copy(txtPrepressFile.Text, "C:\\Test.inx");
?
Avatar of drunk_irishman
drunk_irishman

ASKER

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.
Then, yes best way is dispose stream via 'using' statement.
What is the result of closing the stream? The above example gave two ways to do this.

DaTribe
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.
It also take about 20 minutes for the code to run when I'm doing all the data processing.  I'll report results asap.
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
Ok, the Flush and Close did take care of it.  

Thank you very much for your help, both of you!