I have a program that attempts to write quite a large amount of text to a file on a remote server overseas, which has a slow network connection.
Using the following code, where outputFileContent
is a StringBuilder
:
using (var outfile = new StreamWriter(myRemoteFilePath))
{
outfile.Write(outputFileContent.ToString());
}
it is taking a seriously long time to run (several minutes), whereas if I first write to a local file and then copy it across to the remote location, it is much quicker (20-30 secs):
string tempFilePath = Path.GetTempFileName();
using (var outfile = new StreamWriter(tempFilePath))
{
outfile.Write(outputFileContent.ToString());
}
System.IO.File.Copy(tempFilePath, myRemoteFilePath, true)
Any idea why this is happening? My only guess is that it is something to do with buffering across the network, or perhaps because the stream writer doesn't know how big it needs to be ahead of time.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…