Gần đây tôi đã phải thay đổi từ việc sử dụng File.Copy()
thành CopyFileEx
và tôi đang cố gắng tìm cách sử dụng nó.Thông số trình bao bọc CopyFileEx
Sau rất nhiều googling tôi tìm thấy this wrapper đẹp để sử dụng nó, nhưng những gì tôi cần là để có được sự tiến bộ của byte được sao chép của tập tin hiện tại, và nếu có thể tính toán tiến trình sao chép tất cả các tập tin tôi vượt qua nó .
(Tôi biết có các dự án có thanh tiến trình được liên kết với CopyFileEx
nhưng tôi không đủ kinh nghiệm để rút ra mã có liên quan và tôi muốn sử dụng trình bao bọc này).
Có lẽ chỉ bằng cách so sánh nó với tổng số byte của tệp cần sao chép, mà tôi sẽ tìm thấy trước đó và tính tỷ lệ phần trăm từ đó.
phương pháp hiện tại của tôi sao chép là
FileRoutines.CopyFile(new FileInfo("source.txt"), new FileInfo("dest.txt"));
Những gì tôi đang bị mắc kẹt trên là làm thế nào để quá tải nó với các thông số của cần thiết để có được các thông tin tiến bộ.
public sealed class FileRoutines
{
public static void CopyFile(FileInfo source, FileInfo destination)
{
CopyFile(source, destination, CopyFileOptions.None);
}
public static void CopyFile(FileInfo source, FileInfo destination,
CopyFileOptions options)
{
CopyFile(source, destination, options, null);
}
public static void CopyFile(FileInfo source, FileInfo destination,
CopyFileOptions options, CopyFileCallback callback)
{
CopyFile(source, destination, options, callback, null);
}
public static void CopyFile(FileInfo source, FileInfo destination,
CopyFileOptions options, CopyFileCallback callback, object state)
{
if (source == null) throw new ArgumentNullException("source");
if (destination == null)
throw new ArgumentNullException("destination");
if ((options & ~CopyFileOptions.All) != 0)
throw new ArgumentOutOfRangeException("options");
new FileIOPermission(
FileIOPermissionAccess.Read, source.FullName).Demand();
new FileIOPermission(
FileIOPermissionAccess.Write, destination.FullName).Demand();
CopyProgressRoutine cpr = callback == null ?
null : new CopyProgressRoutine(new CopyProgressData(
source, destination, callback, state).CallbackHandler);
bool cancel = false;
if (!CopyFileEx(source.FullName, destination.FullName, cpr,
IntPtr.Zero, ref cancel, (int)options))
{
throw new IOException(new Win32Exception().Message);
}
}
private class CopyProgressData
{
private FileInfo _source = null;
private FileInfo _destination = null;
private CopyFileCallback _callback = null;
private object _state = null;
public CopyProgressData(FileInfo source, FileInfo destination,
CopyFileCallback callback, object state)
{
_source = source;
_destination = destination;
_callback = callback;
_state = state;
}
public int CallbackHandler(
long totalFileSize, long totalBytesTransferred,
long streamSize, long streamBytesTransferred,
int streamNumber, int callbackReason,
IntPtr sourceFile, IntPtr destinationFile, IntPtr data)
{
return (int)_callback(_source, _destination, _state,
totalFileSize, totalBytesTransferred);
}
}
private delegate int CopyProgressRoutine(
long totalFileSize, long TotalBytesTransferred, long streamSize,
long streamBytesTransferred, int streamNumber, int callbackReason,
IntPtr sourceFile, IntPtr destinationFile, IntPtr data);
[SuppressUnmanagedCodeSecurity]
[DllImport("Kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
private static extern bool CopyFileEx(
string lpExistingFileName, string lpNewFileName,
CopyProgressRoutine lpProgressRoutine,
IntPtr lpData, ref bool pbCancel, int dwCopyFlags);
}
public delegate CopyFileCallbackAction CopyFileCallback(
FileInfo source, FileInfo destination, object state,
long totalFileSize, long totalBytesTransferred);
public enum CopyFileCallbackAction
{
Continue = 0,
Cancel = 1,
Stop = 2,
Quiet = 3
}
[Flags]
public enum CopyFileOptions
{
None = 0x0,
FailIfDestinationExists = 0x1,
Restartable = 0x2,
AllowDecryptedDestination = 0x8,
All = FailIfDestinationExists | Restartable | AllowDecryptedDestination
}
Mọi con trỏ thực sự được đánh giá cao.
Mã này trông giống với mã của bài viết trên Tạp chí MSDN. Nếu vậy, bạn nên ghi có bản gốc. http://msdn.microsoft.com/en-us/magazine/cc163851.aspx –
@ChrisDolan - liên kết chỉ trỏ tới kho lưu trữ tạp chí MSDN - xem chm cho tạp chí MSDN tháng 2 năm 2005 .Net Matters Hình 1 FileRoutines.CopyFile Using Win32 CopyFileEx –
@ TheLonelyCoder - Hmm, bạn nói đúng. Tôi đã tìm thấy nó ở đây: https://web.archive.org/web/20130304214632/http://msdn.microsoft.com/en-us/magazine/cc163851.aspx nhưng không có CSS nên rất khó để đọc –