Is there any difference in using File.Copy to move a file or to record a stream to a location?

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);

+4
source share
3 answers

File.Copy simply encapsulates the use of streams, etc. No difference at all.

+1
source

Well, think about what happens when you start the download and override the existing file, and then for some reason the download will be interrupted, you will have a broken file. however, first downloading it to another location and copying it to the target catalog factors that cause problems.

EDIT: ok, seeing the code now. if the file is already in a MemoryStream, there really is no reason to write the file to a temporary location and copy it. you can just do File.WriteAllBytes(destination,stream.ToArray());

+6
source

It is best to collect the byte file stream in an isolated place and only after it has been assembled in order to copy it to the production area for the following reasons:

  • Suppose that there is a shortage of electricity during the build phase, when it is in an isolated folder, such as "temp", you just get a partial file that you can double-check and ignore later .. however, if you directly assemble the file in production, and there is a power shortage - the next time when you turn on your application, you will need to check the integrity of all files that are not “static” for your application.

  • If the file is used in the production process and the new file is “assembled” long, you end up waiting for your user to complete the assembly process, however, when the buffer assembly process in your example, simply copying the finished file will lead to a shorter waiting period for your user.

  • Suppose the disk space is full ... the same problem as in # 1.

  • Suppose another process in your application has a memory leak and the application is chopped before the build process completes.

  • Suppose [fill out a disaster case] ...

So yes. it’s better to do as in your example, but the real question is: how important is this file for your application? Is it just another data file, for example, a save-game file, or can it crush your application if it is invalid?

+1
source

Source: https://habr.com/ru/post/1478880/


All Articles