Guest User

CopyFileEx

a guest
Nov 21st, 2011
560
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.42 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using System.Runtime.InteropServices;
  7. using System.Security;
  8. using System.ComponentModel;
  9. using System.Security.Permissions;
  10.  
  11. namespace MKV_Chapterizer
  12. {
  13.     public delegate CopyFileCallbackAction CopyFileCallback(string source, string destination, object state, long totalFileSize, long totalBytesTransferred);
  14.  
  15.     public enum CopyFileCallbackAction
  16.     {
  17.         Continue = 0,
  18.         Cancel = 1,
  19.         Stop = 2,
  20.         Quiet = 3
  21.     }
  22.  
  23.     [Flags]
  24.     public enum CopyFileOptions
  25.     {
  26.         None = 0x0,
  27.         FailIfDestinationExists = 0x1,
  28.         Restartable = 0x2,
  29.         AllowDecryptedDestination = 0x8,
  30.         All = FailIfDestinationExists | Restartable | AllowDecryptedDestination
  31.     }
  32.  
  33.     public sealed class AdvancedFileHandling
  34.     {
  35.         private delegate int CopyProgressRoutine(long totalFileSize, long TotalBytesTransferred, long streamSize, long streamBytesTransferred, int streamNumber, int callbackReason, IntPtr sourceFile, IntPtr destinationFile, IntPtr data);
  36.  
  37.         [SuppressUnmanagedCodeSecurity]
  38.         [DllImport("Kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  39.         private static extern bool CopyFileEx(string lpExistingFileName, string lpNewFileName, CopyProgressRoutine lpProgressRoutine, IntPtr lpData, ref bool pbCancel, int dwCopyFlags);
  40.  
  41.         private void CopyAFile()
  42.         {
  43.             CopyFileCallback callback = new CopyFileCallback(ProgressChanged);
  44.             CopyFile("test.txt", "textcopy.txt", CopyFileOptions.None, callback);
  45.         }
  46.  
  47.         private void ProgressChanged(string source, string destination, object state, long totalFileSize, long totalBytesTransferred)
  48.         {
  49.             //Update progressbar here for example
  50.         }
  51.  
  52.         public static void CopyFile(string source, string destination)
  53.         {
  54.             CopyFile(source, destination, CopyFileOptions.None);
  55.         }
  56.  
  57.         public static void CopyFile(string source, string destination, CopyFileOptions options)
  58.         {
  59.             CopyFile(source, destination, options, null);
  60.         }
  61.  
  62.         public static void CopyFile(string source, string destination, CopyFileOptions options, CopyFileCallback callback)
  63.         {
  64.             CopyFile(source, destination, options, callback, null);
  65.         }
  66.  
  67.         public static void CopyFile(string source, string destination, CopyFileOptions options, CopyFileCallback callback, object state)
  68.         {
  69.             if (source == null) throw new ArgumentNullException("source");
  70.             if (destination == null)
  71.                 throw new ArgumentNullException("destination");
  72.             if ((options & ~CopyFileOptions.All) != 0)
  73.                 throw new ArgumentOutOfRangeException("options");
  74.  
  75.             new FileIOPermission(FileIOPermissionAccess.Read, source).Demand();
  76.             new FileIOPermission(FileIOPermissionAccess.Write, destination).Demand();
  77.  
  78.             CopyProgressRoutine cpr = callback == null ?
  79.                 null : new CopyProgressRoutine(new CopyProgressData(source, destination, callback, state).CallbackHandler);
  80.  
  81.             bool cancel = false;
  82.             if (!CopyFileEx(source, destination, cpr, IntPtr.Zero, ref cancel, (int)options))
  83.             {
  84.                 throw new IOException(new Win32Exception().Message);
  85.             }
  86.         }
  87.  
  88.         private class CopyProgressData
  89.         {
  90.             private string _source = null;
  91.             private string _destination = null;
  92.             private CopyFileCallback _callback = null;
  93.             private object _state = null;
  94.  
  95.             public CopyProgressData(string source, string destination,
  96.                 CopyFileCallback callback, object state)
  97.             {
  98.                 _source = source;
  99.                 _destination = destination;
  100.                 _callback = callback;
  101.                 _state = state;
  102.             }
  103.  
  104.             public int CallbackHandler(
  105.                 long totalFileSize, long totalBytesTransferred,
  106.                 long streamSize, long streamBytesTransferred,
  107.                 int streamNumber, int callbackReason,
  108.                 IntPtr sourceFile, IntPtr destinationFile, IntPtr data)
  109.             {
  110.                 return (int)_callback(_source, _destination, _state,
  111.                     totalFileSize, totalBytesTransferred);
  112.             }
  113.         }
  114.     }
  115. }
  116.  
Advertisement
Add Comment
Please, Sign In to add comment