I tried to close the handle in the ProgressRoutine
when the transferred bytes size is greater than or equal to total file length. but the whole operation rarely throws System.IO.IOException : The Handle is invalid.
bool result = CopyFileEx(source, destination, new CopyProgressRoutine(CopyProgressHandler), IntPtr.Zero, ref IsCancelled, copyFileFlags);
if (!result)
{
//when ever we get the result as false it means some error occured so get the last win 32 error.
throw new Win32Exception(Marshal.GetLastWin32Error());
}
here is the CopyProgressHandler
method
private CopyProgressResult CopyProgressHandler(long total, long transferred, long streamSize, long streamByteTrans, uint dwStreamNumber,
CopyProgressCallbackReason reason, IntPtr hSourceFile, IntPtr hDestinationFile, IntPtr lpData)
{
//when a chunk is finished call the progress changed.
if (reason == CopyProgressCallbackReason.CALLBACK_CHUNK_FINISHED)
{
OnProgressChanged((transferred / (double)total) * 100.0);
}
//transfer completed
if (transferred >= total)
{
if (CloseHandle(hDestinationFile)) // here is that valid!! ?
{
//if file is read only, remove read-only attribute(case to handle CD drive import)
FileAttributes attr = File.GetAttributes(destinationFilePath);
if ((attr & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
{
attr = attr & ~FileAttributes.ReadOnly;
File.SetAttributes(destinationFilePath, attr);
}
OnCompleted(CopyCompletedType.Succeeded, null);
}
else
{
OnCompleted(CopyCompletedType.Exception,
new System.IO.IOException("Unable to close the file handle"));
}
}
return CopyProgressResult.PROGRESS_CONTINUE;
}
[System.Runtime.InteropServices.DllImport("Kernel32")]
private extern static Boolean CloseHandle(IntPtr handle);
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool CopyFileEx(string lpExistingFileName, string lpNewFileName, CopyProgressRoutine lpProgressRoutine, IntPtr lpData, ref Int32 pbCancel, CopyFileFlags dwCopyFlags);
private delegate CopyProgressResult CopyProgressRoutine(long TotalFileSize, long TotalBytesTransferred, long StreamSize, long StreamBytesTransferred, uint dwStreamNumber, CopyProgressCallbackReason dwCallbackReason,
IntPtr hSourceFile, IntPtr hDestinationFile, IntPtr lpData);
private enum CopyProgressResult : uint
{
PROGRESS_CONTINUE = 0,
PROGRESS_CANCEL = 1,
PROGRESS_STOP = 2,
PROGRESS_QUIET = 3
}
private enum CopyProgressCallbackReason : uint
{
CALLBACK_CHUNK_FINISHED = 0x00000000,
CALLBACK_STREAM_SWITCH = 0x00000001
}
[Flags]
private enum CopyFileFlags : uint
{
COPY_FILE_FAIL_IF_EXISTS = 0x00000001,
COPY_FILE_NO_BUFFERING = 0x00001000,
COPY_FILE_RESTARTABLE = 0x00000002,
COPY_FILE_OPEN_SOURCE_FOR_WRITE = 0x00000004,
COPY_FILE_ALLOW_DECRYPTED_DESTINATION = 0x00000008
}
if that is valid, why the error ?
Do we really need to close the handle or it will be closed by CopyFileEx
? but if I removed the code CloseHandle
i never get this exception. But the destination file is used by another operation after this copy, Thats why I equipped this.!
Please help to avoid the System.IO.IOException : The handle is invalid
error.
You must not close the handles. The handles are owned by CopyFileEx
and it closes them. Remove the calls to CloseHandle
.