What sizes should I copy files?

Our .NET application copies large files and requires user feedback; therefore, instead of using it File.Copy, we read a fragment of one file and write it to another, and show the progress after each fragment. Nothing unusual. But what is the correct chunk size to use to give the fastest copy of the file, assuming the time to display progress is negligible?

+3
source share
3 answers

You should consider using the win32 function CopyFileTransacted (Vista only) or CopyFileEx (Windows 2000 and later). They are provided by Windows and are optimized for speed.

I would recommend you test your own C # implementation performance and compare it with File.Copy's own performance. If the performance is comparable (i.e. the same order) than go with your custom C # implementation. Otherwise, it is better to use the CopyFileTransacted or CopyFileEx function.

Ps from here :

[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("kernel32.dll", CharSet=CharSet.Unicode, SetLastError=true)]
internal static extern bool CopyFileTransacted([In] string lpExistingFileName, [In] string lpNewFileName, [In] IntPtr lpProgressRoutine, [In] IntPtr lpData, [In, MarshalAs(UnmanagedType.Bool)] ref bool pbCancel, [In] CopyFileFlags dwCopyFlags, [In] KtmTransactionHandle hTransaction);
+2
source

, . , .

- 64 , .

, , 512 (1 )

+2

Even when using large chunks, you can easily evaluate progress to give the user "fake" feedback. Depending on the application, why don't you let the user set the size? Give the user an option if he wants to :)

0
source

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


All Articles