예제 #1
0
 private async Task CopyFile(string source, string target, CancellationToken token)
 {
     Kernel32.CopyProgressRoutine progress = delegate {
         return(token.IsCancellationRequested ? 1 : 0);
     };
     token.ThrowIfCancellationRequested();
     await Task.Run(
         () => {
         bool ok = Kernel32.CopyFileEx(source, target, progress, 0x00000800 | 0x00000001, false);
         if (!ok)
         {
             token.ThrowIfCancellationRequested();
         }
     }, token
         );
 }
        public static void CopyFile(string source, string destination,
                                    Kernel32.CopyFileOptions options, Kernel32.CopyFileCallback callback, object state)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }
            if (destination == null)
            {
                throw new ArgumentNullException("destination");
            }
            if ((options & ~Kernel32.CopyFileOptions.All) != 0)
            {
                throw new ArgumentOutOfRangeException("options");
            }

            //new FileIOPermission(FileIOPermissionAccess.Read, source).Demand();
            //new FileIOPermission(FileIOPermissionAccess.Write, destination).Demand();

            if (new FileInfo(source).Length <= FILESIZE_TRANSFER_ATOM)
            {
                File.Copy(source, destination, true);
            }
            else
            {
                Kernel32.CopyProgressRoutine cpr = callback == null ?
                                                   null : new Kernel32.CopyProgressRoutine(new Kernel32.CopyProgressData(
                                                                                               source, destination, callback, state).CallbackHandler);

                bool cancel = false;
                if (!Kernel32.CopyFileEx(source, destination, cpr, IntPtr.Zero, ref cancel, (int)options))
                {
                    int err = Kernel32.GetLastError();
                    if (err != WinError.S_OK)
                    {
                        throw new Win32Exception(err);
                    }
                }
            }
        }