Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
Hello there.
 
While coding in an application that continuosly logs strings to a text file, I noticed that at some point, the IO.StreamWriter class will simply stop writing, even if it's in the middle of a line. Investigating further, I found out that this also happens when writing one long string at once, as this example code does:
Stream = New IO.FileStream(FilePath, _
                           IO.FileMode.Create, _
                           IO.FileAccess.Write)
Writer = New IO.StreamWriter(Stream)
 
Writer.Write(Space(30000))
If I open the text file afterwards, instead of the expected 30000 spaces, I find that it contains only 29696. Interestingly, no matter how many characters I try to write, it always seems to be a multiple of 1024 that actually is written.
 
Does somebody have an idea what the issue could be?
Posted 13-Feb-13 3:54am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Problem solved Smile | :) . Instead of closing the stream after the string is written, the writer has to be closed. Otherwise, the writer will only write the size of its buffer that apparently is 1024.
 
It works fine this way:
Stream = New IO.FileStream(FilePath, _
                           IO.FileMode.Create, _
                           IO.FileAccess.Write)
Writer = New IO.StreamWriter(Stream)
 
Writer.Write(Space(30000))
Writer.Close()
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web02 | 2.8.140926.1 | Last Updated 13 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100