Efficient way of writing to a text file in VB.NET

AZhu picture AZhu · Jan 24, 2011 · Viewed 19.4k times · Source

We have some information that we need to write (about 18 KB) to a .txt file stored on one of our network drives. The file is re-written about once every 15 minutes, but it is read practically at least every second. We are currently using StreamWriter to write the file.

The file server is in a remote location and the round trip ping varies from less than 1 ms to 15 ms.

The problem is, sometimes it takes as long as six seconds to write the contents to the file, which is definitely way too long even after we take consideration of the network speed.

Therefore, I am just wondering if there is any efficient way to write the file using VB.NET to improve the performance? Java has a very good tool named BufferedOutputStream, which unfortunately is not available in VB.NET (or I just have not found it).

Answer

Henk Holterman picture Henk Holterman · Jan 24, 2011

The fastest option:

Collect all your text into one large string first and then use System.IO.File.WriteAllText(text).