reemplazar net duplicar copia archivos archivo c# .net copy fileinfo

net - reemplazar archivo c#



¿Puedo mostrar el progreso de la copia de archivo usando FileInfo.CopyTo() en.NET? (6)

FileInfo.CopyTo es básicamente un contenedor alrededor de la llamada API de Win32 "CopyFile" en kernel32.dll. Este método no admite la devolución de llamada de progreso.

Sin embargo, el método CopyFileEx lo hace, y usted puede escribir su propio contenedor .NET a su alrededor en unos minutos, como se describe aquí: http://www.pinvoke.net/default.aspx/kernel32.CopyFileEx

Creé una utilidad de copia en c # (.NET 2.0 Framework) que copia archivos, directorios y subdirectorios recursivos, etc. El programa tiene una GUI que muestra el archivo actual que se está copiando, el número de archivo actual (secuencia), el número total de los archivos que se copiarán y el porcentaje completado para las operaciones de copia. También hay una barra de progreso, que se basa en el archivo actual / archivos totales.

Mi problema está relacionado con la copia de archivos de gran tamaño. No he podido encontrar una forma de indicar el progreso total de copia de un archivo grande (utilizando mi estructura de clase actual que utiliza el método FileInfo.CopyTo). Como solución, he separado las operaciones de copia de archivos y la visualización de la GUI de sus propios hilos y configuré una entrada visual para mostrar que se está trabajando. Al menos, el usuario es consciente de que el programa no está congelado y aún está copiando archivos.

Sería mejor poder mostrar el progreso basado en el número total de bytes o tener algún tipo de evento que se active desde el método FileInfo.CopyTo que indica el número total de bytes copiados del archivo actual.

Soy consciente de la propiedad FileInfo.Length, así que estoy seguro de que MacGuyver tiene una forma de mi propio evento que se basa en esto y que tiene un controlador en el lado de la interfaz gráfica de usuario que lee las actualizaciones (tal vez basado en la comprobación de FileInfo. ¿Propiedad de longitud del objeto de destino usando algún tipo de temporizador?).

¿Alguien sabe de una manera de hacer esto que estoy pasando por alto. Si puedo evitarlo, prefiero no reescribir mi clase para copiar bytes a través de una secuencia y seguirlo de esa manera (aunque estoy pensando que podría estar bloqueado por seguir esa ruta).

Gracias por adelantado

PD: estoy atrapado en el marco .NET 2.0 por ahora, por lo que cualquier solución que requiera funciones disponibles en> = 3.0 solo no es una opción para mí.

PPS: estoy abierto a soluciones en cualquier variedad de lenguaje .NET, no solo c #.


Gracias a @Gasper y @Dennis por señalar el método CopyFileEx. He extendido la respuesta de Dennis con una copia abortiva

/// <summary> /// Type indicates how the copy gets completed. /// </summary> internal enum CopyCompletedType { Succeeded, Aborted, Exception } /// <summary> /// Event arguments for file copy /// </summary> internal class FileCopyEventArgs : EventArgs { /// <summary> /// Constructor /// </summary> /// <param name="type">type of the copy completed type enum</param> /// <param name="exception">exception if any</param> public FileCopyEventArgs(CopyCompletedType type, Exception exception) { Type = type; Exception = exception; } /// <summary> /// Type of the copy completed type /// </summary> public CopyCompletedType Type { get; private set; } /// <summary> /// Exception if any happend during copy. /// </summary> public Exception Exception { get; private set; } } /// <summary> /// PInvoke wrapper for CopyEx /// http://msdn.microsoft.com/en-us/library/windows/desktop/aa363852.aspx /// </summary> internal class XCopy { private int IsCancelled; private int FilePercentCompleted; public XCopy() { IsCancelled = 0; } /// <summary> /// Copies the file asynchronously /// </summary> /// <param name="source">the source path</param> /// <param name="destination">the destination path</param> /// <param name="nobuffering">Bufferig status</param> /// <param name="handler">Event handler to do file copy.</param> public void CopyAsync(string source, string destination, bool nobuffering) { try { //since we needed an async copy .. Action action = new Action( () => CopyInternal(source, destination, nobuffering) ); Task task = new Task(action); task.Start(); } catch (AggregateException ex) { //handle the inner exception since exception thrown from task are wrapped in //aggreate exception. OnCompleted(CopyCompletedType.Exception, ex.InnerException); } catch (Exception ex) { OnCompleted(CopyCompletedType.Exception, ex); } } /// <summary> /// Event which will notify the subscribers if the copy gets completed /// There are three scenarios in which completed event will be thrown when /// 1.Copy succeeded /// 2.Copy aborted. /// 3.Any exception occured. /// These information can be obtained from the Event args. /// </summary> public event EventHandler<FileCopyEventArgs> Completed; /// <summary> /// Event which will notify the subscribers if there is any progress change while copying. /// This will indicate the progress percentage in its event args. /// </summary> public event EventHandler<ProgressChangedEventArgs> ProgressChanged; /// <summary> /// Aborts the copy asynchronously and throws Completed event when done. /// User may not want to wait for completed event in case of Abort since /// the event will tell that copy has been aborted. /// </summary> public void AbortCopyAsync() { Trace.WriteLine("Aborting the copy"); //setting this will cancel an operation since we pass the //reference to copyfileex and it will periodically check for this. //otherwise also We can check for iscancelled on onprogresschanged and return //Progress_cancelled . IsCancelled = 1; Action completedEvent = new Action(() => { //wait for some time because we ll not know when IsCancelled is set , at what time windows stops copying. //so after sometime this may become valid . Thread.Sleep(500); //do we need to wait for some time and send completed event. OnCompleted(CopyCompletedType.Aborted); //reset the value , otherwise if we try to copy again since value is 1 , //it thinks that its aborted and wont allow to copy. IsCancelled = 0; }); Task completedTask = new Task(completedEvent); completedTask.Start(); } /// <summary> /// Copies the file using asynchronos task /// </summary> /// <param name="source">the source path</param> /// <param name="destination">the destination path</param> /// <param name="nobuffering">Buffering status</param> /// <param name="handler">Delegate to handle Progress changed</param> private void CopyInternal(string source, string destination, bool nobuffering) { CopyFileFlags copyFileFlags = CopyFileFlags.COPY_FILE_RESTARTABLE; if (nobuffering) { copyFileFlags |= CopyFileFlags.COPY_FILE_NO_BUFFERING; } try { Trace.WriteLine("File copy started with Source: " + source + " and destination: " + destination); //call win32 api. 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()); } } catch (Exception ex) { //the mesage will contain the requested operation was aborted when the file copy //was cancelled. so we explicitly check for that and do a graceful exit if (ex.Message.Contains("aborted")) { Trace.WriteLine("Copy aborted."); } else { OnCompleted(CopyCompletedType.Exception, ex.InnerException); } } } private void OnProgressChanged(double percent) { // only raise an event when progress has changed if ((int)percent > FilePercentCompleted) { FilePercentCompleted = (int)percent; var handler = ProgressChanged; if (handler != null) { handler(this, new ProgressChangedEventArgs((int)FilePercentCompleted, null)); } } } private void OnCompleted(CopyCompletedType type, Exception exception = null) { var handler = Completed; if (handler != null) { handler(this, new FileCopyEventArgs(type, exception)); } } #region PInvoke /// <summary> /// Delegate which will be called by Win32 API for progress change /// </summary> /// <param name="total">the total size</param> /// <param name="transferred">the transferrred size</param> /// <param name="streamSize">size of the stream</param> /// <param name="streamByteTrans"></param> /// <param name="dwStreamNumber">stream number</param> /// <param name="reason">reason for callback</param> /// <param name="hSourceFile">the source file handle</param> /// <param name="hDestinationFile">the destination file handle</param> /// <param name="lpData">data passed by users</param> /// <returns>indicating whether to continue or do somthing else.</returns> 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)) { 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 } #endregion }

Los clientes pueden crear un objeto de la clase XCopy y llamar al método copiar / abortar.


Para este tipo de cosas, he recurrido a Shell32 (¿o es ShellUI? Ya no sé). Esto le brinda un diálogo nativo de Windows que los usuarios están acostumbrados a ver para las operaciones de copiado. Supongo que reemplazará el cuadro de diálogo existente, por lo que puede que no sea la respuesta correcta para usted, pero es útil recordarlo para aquellos escenarios "en apuros".

Microsoft.VisualBasic.FileIO.FileSystem.CopyFile( srcPath, dstPath, Microsoft.VisualBasic.FileIO.UIOption.AllDialogs, Microsoft.VisualBasic.FileIO.UICancelOption.ThrowException );

Sí, debe hacer referencia al ensamblado Microsoft.VisualBasic. He llegado a love esta asamblea.


Por el amor de Dios, ¡no implemente su propia copia de archivo usando streams! La llamada Win32 CopyFile API que mencionó Gaspar es capaz de aprovechar, por ejemplo, DMA, mientras que apostaría dólares a donuts que el código Will no sería lo suficientemente "inteligente" como para hacer eso.

CopyFileEx lo tratará bien, o podría implementar un BackgroundWorker que observe el tamaño creciente del archivo de destino y actualice una barra de progreso usando esa información. El último método le ahorra un PInvoke, pero el primero es probablemente un poco más limpio a largo plazo.


Sé que llegué un poco tarde a la fiesta, pero hice un contenedor para CopyFileEx que devuelve una Task y acepta un CancellationToken y un IProgress<double> . Desafortunadamente no funcionará en el marco .NET 2.0, pero para cualquiera que use 4.5, esto le permite usar la palabra clave await .

public static class FileEx { public static Task CopyAsync(string sourceFileName, string destFileName) { return CopyAsync(sourceFileName, destFileName, CancellationToken.None); } public static Task CopyAsync(string sourceFileName, string destFileName, CancellationToken token) { return CopyAsync(sourceFileName, destFileName, token, null); } public static Task CopyAsync(string sourceFileName, string destFileName, IProgress<double> progress) { return CopyAsync(sourceFileName, destFileName, CancellationToken.None, progress); } public static Task CopyAsync(string sourceFileName, string destFileName, CancellationToken token, IProgress<double> progress) { int pbCancel = 0; CopyProgressRoutine copyProgressHandler; if (progress != null) { copyProgressHandler = (total, transferred, streamSize, streamByteTrans, dwStreamNumber, reason, hSourceFile, hDestinationFile, lpData) => { progress.Report((double)transferred / total * 100); return CopyProgressResult.PROGRESS_CONTINUE; }; } else { copyProgressHandler = EmptyCopyProgressHandler; } token.ThrowIfCancellationRequested(); var ctr = token.Register(() => pbCancel = 1); var copyTask = Task.Run(() => { try { CopyFileEx(sourceFileName, destFileName, copyProgressHandler, IntPtr.Zero, ref pbCancel, CopyFileFlags.COPY_FILE_RESTARTABLE); token.ThrowIfCancellationRequested(); } finally { ctr.Dispose(); } }, token); return copyTask; } private static CopyProgressResult EmptyCopyProgressHandler(long total, long transferred, long streamSize, long streamByteTrans, uint dwStreamNumber, CopyProgressCallbackReason reason, IntPtr hSourceFile, IntPtr hDestinationFile, IntPtr lpData) { return CopyProgressResult.PROGRESS_CONTINUE; } #region DLL Import [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); delegate CopyProgressResult CopyProgressRoutine( long totalFileSize, long totalBytesTransferred, long streamSize, long streamBytesTransferred, uint dwStreamNumber, CopyProgressCallbackReason dwCallbackReason, IntPtr hSourceFile, IntPtr hDestinationFile, IntPtr lpData); enum CopyProgressResult : uint { PROGRESS_CONTINUE = 0, PROGRESS_CANCEL = 1, PROGRESS_STOP = 2, PROGRESS_QUIET = 3 } enum CopyProgressCallbackReason : uint { CALLBACK_CHUNK_FINISHED = 0x00000000, CALLBACK_STREAM_SWITCH = 0x00000001 } [Flags] enum CopyFileFlags : uint { COPY_FILE_FAIL_IF_EXISTS = 0x00000001, COPY_FILE_RESTARTABLE = 0x00000002, COPY_FILE_OPEN_SOURCE_FOR_WRITE = 0x00000004, COPY_FILE_ALLOW_DECRYPTED_DESTINATION = 0x00000008 } #endregion }


También utilicé la implementación provista en la respuesta marcada . Sin embargo, luego creé un contenedor para proporcionar una API de nicer ™ para usar desde .NET.

Uso:

XCopy.Copy(networkFile.FullPath, temporaryFilename, true, true, (o, pce) => { worker.ReportProgress(pce.ProgressPercentage, networkFile); });

Implementación

/// <summary> /// PInvoke wrapper for CopyEx /// http://msdn.microsoft.com/en-us/library/windows/desktop/aa363852.aspx /// </summary> public class XCopy { public static void Copy(string source, string destination, bool overwrite, bool nobuffering) { new XCopy().CopyInternal(source, destination, overwrite, nobuffering, null); } public static void Copy(string source, string destination, bool overwrite, bool nobuffering, EventHandler<ProgressChangedEventArgs> handler) { new XCopy().CopyInternal(source, destination, overwrite, nobuffering, handler); } private event EventHandler Completed; private event EventHandler<ProgressChangedEventArgs> ProgressChanged; private int IsCancelled; private int FilePercentCompleted; private string Source; private string Destination; private XCopy() { IsCancelled = 0; } private void CopyInternal(string source, string destination, bool overwrite, bool nobuffering, EventHandler<ProgressChangedEventArgs> handler) { try { CopyFileFlags copyFileFlags = CopyFileFlags.COPY_FILE_RESTARTABLE; if (!overwrite) copyFileFlags |= CopyFileFlags.COPY_FILE_FAIL_IF_EXISTS; if (nobuffering) copyFileFlags |= CopyFileFlags.COPY_FILE_NO_BUFFERING; Source = source; Destination = destination; if (handler != null) ProgressChanged += handler; bool result = CopyFileEx(Source, Destination, new CopyProgressRoutine(CopyProgressHandler), IntPtr.Zero, ref IsCancelled, copyFileFlags); if (!result) throw new Win32Exception(Marshal.GetLastWin32Error()); } catch (Exception) { if (handler != null) ProgressChanged -= handler; throw; } } private void OnProgressChanged(double percent) { // only raise an event when progress has changed if ((int)percent > FilePercentCompleted) { FilePercentCompleted = (int)percent; var handler = ProgressChanged; if (handler != null) handler(this, new ProgressChangedEventArgs((int)FilePercentCompleted, null)); } } private void OnCompleted() { var handler = Completed; if (handler != null) handler(this, EventArgs.Empty); } #region PInvoke [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 } private CopyProgressResult CopyProgressHandler(long total, long transferred, long streamSize, long streamByteTrans, uint dwStreamNumber, CopyProgressCallbackReason reason, IntPtr hSourceFile, IntPtr hDestinationFile, IntPtr lpData) { if (reason == CopyProgressCallbackReason.CALLBACK_CHUNK_FINISHED) OnProgressChanged((transferred / (double)total) * 100.0); if (transferred >= total) OnCompleted(); return CopyProgressResult.PROGRESS_CONTINUE; } #endregion }