I'm refinancing some code, I have a question that I can use a few comments for.
The original code loads the file into the stream. It then writes the stream to a file in the temp directory before using File.Copy to overwrite the existing file in the production directory.
Are there any advantages associated with writing it to a temporary directory and using File.Copy, as opposed to just writing the stream to the production directory right away?
One reason may be that File.Copy writes the stream faster and reduces the likelihood that someone will read the file while it is being written. But can this happen? What else should I keep in mind. I am considering factoring from the temp directory.
MemoryStream stream = new MemoryStream(); ....Download and valiate stream.... using (Stream sourceFileStream = stream) { using (FileStream targetFileStream = new FileStream(tempPath, FileMode.CreateNew)) { const int bufferSize = 8192; byte[] buffer = new byte[bufferSize]; while (true) { int read = sourceFileStream.Read(buffer, 0, bufferSize); targetFileStream.Write(buffer, 0, read); if (read == 0) break; } } } File.Copy(tempPath, destination, true);
unlike just writing a stream to a destination.
This is only the code that I had, I would correctly use something like sourceFileStream.CopyToAsync(TargetFileStream);
source share