Link to home
Start Free TrialLog in
Avatar of Jimbo99999
Jimbo99999Flag for United States of America

asked on

VB.Net - IO.StreamWriter File Display Size

Good Day Experts!

I have a little project that writes out data to a csv file via a StreamWriter.  When I go to the directory that the file is created in,  the size of the file does not increase as the file grows. I have to hit View --> Refresh.

Is there a way to have the size change dynamically so I don't have to hit View --> Refresh?

Thanks,
jimbo99999
Avatar of kaufmed
kaufmed
Flag of United States of America image

That's due to Windows, not your application. There may be a way to hook into Explorer's API (if it has one), but I suspect it would be more trouble than it's worth. For that matter, you might just do a SendKeys call to the Explorer window and send an F5 key press. You would need the SetForegroundWindow Win API method. You would also need a way of identifying the target Explorer window (if you have multiples open). I think for this you would want to spawn the Explorer window yourself (from code) so that you can capture it's window handle--which SetForegroundWindow would need.
ASKER CERTIFIED SOLUTION
Avatar of Carl Tawn
Carl Tawn
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
Avatar of Jimbo99999

ASKER

Thanks for the replies.

At the bottom of my Do While Loop I Have:

out.WriteLine(DTLString)

You are suggesting I flush?

jimbo99999
You still need the WriteLine() there to write out the data. The output is normally buffered (i.e. held in memory rather than being immediately committed to disk), so adding a out.Flush() call periodically inside your loop should force the content to disk and allows Windows to update to show the current actual size.

You don't want to use Flush() on every iteration though, pick some sensible period (say once for every 10% of iterations)
Doh! I forgot about that. (No more questions at 1 AM for me.) carl_tawn is correct; disregard my previous comment.
Ok, up above my loop where I define the Streamwriter here is what I am trying:

out = My.Computer.FileSystem.OpenTextFileWriter(FileName, False)
out.AutoFlush = True

It is incrementing the size now with no View --> Refresh.  

Do you see any issues with doing it this way?

Thanks,
jimbo99999
No, that's fine and dandy. It will have the same effect as doing it manually.
Thanks for the help.  Until today I never knew what .Flush was for.

Have a good day,
jimbo99999