Function code File.Copy:
public static void Copy(string sourceFileName, string destFileName) { if (sourceFileName == null) throw new ArgumentNullException("sourceFileName", Environment.GetResourceString("ArgumentNull_FileName")); if (destFileName == null) throw new ArgumentNullException("destFileName", Environment.GetResourceString("ArgumentNull_FileName")); if (sourceFileName.Length == 0) throw new ArgumentException(Environment.GetResourceString("Argument_EmptyFileName"), "sourceFileName"); if (destFileName.Length == 0) throw new ArgumentException(Environment.GetResourceString("Argument_EmptyFileName"), "destFileName"); File.InternalCopy(sourceFileName, destFileName, false, true); }
from where it is called:
internal static string InternalCopy(string sourceFileName, string destFileName, bool overwrite, bool checkHost) { string fullPathInternal1 = Path.GetFullPathInternal(sourceFileName); string fullPathInternal2 = Path.GetFullPathInternal(destFileName); FileIOPermission.QuickDemand(FileIOPermissionAccess.Read, fullPathInternal1, false, false); FileIOPermission.QuickDemand(FileIOPermissionAccess.Write, fullPathInternal2, false, false); if (!Win32Native.CopyFile(fullPathInternal1, fullPathInternal2, !overwrite)) { int lastWin32Error = Marshal.GetLastWin32Error(); string maybeFullPath = destFileName; if (lastWin32Error != 80) { using (SafeFileHandle file = Win32Native.UnsafeCreateFile(fullPathInternal1, int.MinValue, FileShare.Read, (Win32Native.SECURITY_ATTRIBUTES) null, FileMode.Open, 0, IntPtr.Zero)) { if (file.IsInvalid) maybeFullPath = sourceFileName; } if (lastWin32Error == 5 && Directory.InternalExists(fullPathInternal2)) throw new IOException(Environment.GetResourceString("Arg_FileIsDirectory_Name", (object) destFileName), 5, fullPathInternal2); } __Error.WinIOError(lastWin32Error, maybeFullPath); } return fullPathInternal2; }
As you can see, the Win32 function CopyFile is used, in connection with this, when the File.Copy function has already finished its work, the file could in fact not be copied by the operating system. When you directly copy through streams, this does not happen, apparently, copying occurs immediately. I hope someone will explain in more detail.