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

The fastest option:

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


Consider these in turn:

  1. Create the file locally
  2. Copy it to remote folder with a temporary extension
  3. Rename the remote file to your original file name

Step 2 and 3 is as follows (using System.IO):

string OriginalExtension = ".ok", TemporaryExtension = ".dat";
string tmpFileRemote = RemoteFile.Replace(TemporaryExtension, OriginalExtension);
File.Copy(fileName, RemoteFile, true);
File.Copy(RemoteFile, tmpFileRemote, true);
File.Delete(RemoteFile);

The first File.Copy takes the time. But since it doesn't lock the real file people are using, it doesn't get locked. The second File.Copy actually just renames the file and replaces the real file with the one just uploaded. File.Delete deletes the temporary file uploaded.

Hope that helps.