我有一个程序试图将大量文本写入海外远程服务器上的文件,该服务器的网络连接速度很慢。
使用以下代码,其中outputFileContent
a StringBuilder
:
using (var outfile = new StreamWriter(myRemoteFilePath))
{
outfile.Write(outputFileContent.ToString());
}
运行需要很长时间(几分钟),而如果我先写入本地文件,然后将其复制到远程位置,则速度要快得多(20-30 秒):
string tempFilePath = Path.GetTempFileName();
using (var outfile = new StreamWriter(tempFilePath))
{
outfile.Write(outputFileContent.ToString());
}
System.IO.File.Copy(tempFilePath, myRemoteFilePath, true)
知道为什么会这样吗?我唯一的猜测是这与通过网络进行缓冲有关,或者可能是因为流编写器不知道它需要提前多大。