Does File.Copy return only after copying a file?

I could not find the answer to this question and was not sure how to test it.

If I have the following two lines:

TextWriter tw = new StreamWriter(txtPath,true);

File.Copy(OriginalPath, DestinationPath));

tw.WriteLine("blah");

Does the 3rd line remain until File.Copy is fully completed or the 3rd line is started while the File.Copy file is still running?

+4
source share
4 answers

Yes, he will wait, because it File.Copy()is a copy operation of a synchronous method / file.

You can download Visual Studio 2013 Express Edition for Windows Desktop for free to test this code in C #. Download link

Visual Studio Main(). 5-10 , , Copy() .

+8

, , File.Copy - "" , , , . , , , . (, , ); File.Copy, , , . SSD.

+7

. - , -, , (.. Task, Task<T> IAsyncResult)

File.Copyis a method voidthat returns nothing, so it is expected that it will complete its work when it returns.

+3
source

According to ILSpy, the method File.Copyinternally calls the Win32 function CopyFileat some point, which in turn is a synchronous function.

So, in my opinion, it is fully synchronized, not asynchronous. Thus, your 3rd line is called after a complete copy of the file.

+1
source

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


All Articles