FileInfo.Length seems to be not working as advertised

Hi,
I have a C# program where I am writing out data to a tab delimited file and I want the program to ascertain when the file size reaches 5MB and save it and start a new one.

In my code I have this:

                           FileInfo f = new FileInfo(opFolder + "\\" + opFile);
                            if (f != null)
                            {
                                //If length of file is greater than 5MB write it out and create a new version.
                                if(f.Length > 5242880)
                                {
                                     //File save and start a new one here ...
                                }
                            }

For some unknown reason my file seems to keep on growing to around 50MB before a new one is started.  Is this an error in the .Length property or am I missing something important!?

In the MSDN documentation it says the .Length property returns the file size in bytes??


Siv
SivAsked:
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.

Jacques Bourgeois (James Burger)PresidentCommented:
Is this all your code?

Are you doing something with the file between the moment you instantiate the object and measure its length. Is many situations, the value of the Length is cached and is not updated if the file changes size while the object lives. You might try to call the Refresh method on the object before testing it's Length.
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
SivAuthor Commented:
Jacques/James,

This is all the code, obviously I have removed the stuff between the braces that saves the file and then closes it and then starts a new one.

I will try that as it had not occurred that the file info might be cached.

One other possibility now that I am scrutinising the code a lot more is that I may be that I have placed the test for size at a point in the code where it is only reached after a number of loops around so that the size can have got a lot larger than 50MB before I test it!

Siv
0
SivAuthor Commented:
The caching was the issue. Sorry for late reply as I have been mega busy with the build up to and then deployment of Windows 10 so not had a moment to call back here.

Thanks for your help!

Siv
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
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.