public CopyProgressData(FileInfo source, FileInfo destination, CopyFileCallback callback, object state) { _source = source; _destination = destination; _callback = callback; _state = state; }
public static void CopyFile(string source, string 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).Demand(); new FileIOPermission(FileIOPermissionAccess.Write, destination).Demand(); CopyProgressRoutine cpr = callback == null ? null : new CopyProgressRoutine(new CopyProgressData(source, destination, callback, state).CallbackHandler); bool cancel = false; if (!CopyFileEx(source, destination, cpr, IntPtr.Zero, ref cancel, (int)options)) { throw new IOException(new Win32Exception().Message); } }
public CopyProgressData(FileInfo source, FileInfo destination, CopyFileCallback callback, object state) { this.source = source; this.destination = destination; this.callback = callback; this.state = state; }
public static void MoveFile(FileInfo source, FileInfo destination, MoveFileOptions options, CopyFileCallback callback) { if (source == null) { throw new ArgumentNullException("source"); } if (destination == null) { throw new ArgumentNullException("destination"); } if ((options & ~MoveFileOptions.All) != 0) { throw new ArgumentOutOfRangeException("options"); } new FileIOPermission(FileIOPermissionAccess.Write, source.FullName).Demand(); new FileIOPermission(FileIOPermissionAccess.Write, destination.FullName).Demand(); object state = null; CopyProgressRoutine cpr = (callback == null) ? null : new CopyProgressRoutine(new CopyProgressData( source, destination, callback, state).CallbackHandler); if (!NativeMethods.MoveFileWithProgress(source.FullName, destination.FullName, cpr, IntPtr.Zero, (int)options)) { throw new IOException(new Win32Exception().Message); } }
public CopyProgressData(string source, string destination, CopyFileCallback callback) { _source = source; _destination = destination; _callback = callback; }
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); } }
public CopyProgressData(string source, string destination, CopyFileCallback callback, object state) { _source = source; _destination = destination; _callback = callback; _state = state; }
/// <summary> /// Creates a new instance of <see cref="CopyFileExWrapperData"/> /// </summary> /// <param name="source">The source file</param> /// <param name="destination">The destination file</param> /// <param name="options">The options</param> /// <param name="callback">The operation callback</param> /// <param name="state">The operation state</param> /// <param name="cancellationToken">The cancellation token</param> public CopyFileExWrapperData(string source, string destination, CopyFileOptions options, CopyFileCallback callback, object state, CancellationToken cancellationToken) { Source = source; Destination = destination; Options = options; Callback = callback; State = state; CancellationToken = cancellationToken; }
public static void MoveFile(FileInfo source, FileInfo destination, MoveFileOptions options, CopyFileCallback callback) { if (source == null) throw new ArgumentNullException("source"); if (destination == null) throw new ArgumentNullException("destination"); 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, null).CallbackHandler)); if (!MoveFileWithProgress(source.FullName, destination.FullName, cpr, IntPtr.Zero, options)) { throw new IOException(new Win32Exception().Message); } }
public static void MoveFile(FileInfo source, FileInfo destination, MoveFileOptions options, CopyFileCallback callback) { if (source == null) { throw new ArgumentNullException("source"); } if (destination == null) { throw new ArgumentNullException("destination"); } 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, null).CallbackHandler)); if (!MoveFileWithProgress(source.FullName, destination.FullName, cpr, IntPtr.Zero, options)) { throw new IOException(new Win32Exception().Message); } }
// ReSharper disable once FlagArgument private static void CopyFile(FileInfo source, FileInfo destination, CopyFileOptions options, CopyFileCallback callback, object state) { if (source == null) { throw new ArgumentNullException(nameof(source)); } if (destination == null) { throw new ArgumentNullException(nameof(destination)); } if ((options & ~CopyFileOptions.ALL) != 0) { throw new ArgumentOutOfRangeException(nameof(options)); } var copyProgressRoutine = callback == null ? null : new CopyProgressRoutine(new CopyProgressData(source, destination, callback, state).CallbackHandler); bool cancel = false; if (!CopyFileEx(source.FullName, destination.FullName, copyProgressRoutine, IntPtr.Zero, ref cancel, (int)options)) { throw new IOException(new Win32Exception().Message); } }
public static void CopyFile(FileInfo source, FileInfo destination, CopyFileOptions options, CopyFileCallback callback) { CopyFile(source, destination, options, callback, null); }
private void HandleFinishedFile(string path, string newpath, bool overwrite) { string inputDrive = Path.GetPathRoot(path); string outputDrive = Path.GetPathRoot(newpath); if (inputDrive == outputDrive) { WriteLog("Deleting the input file"); File.Delete(newpath); WriteLog("Moving the finished file"); File.Move(path, newpath); return; } WriteLog("Starting to copy the finished file"); //Copy the file CopyFileCallback callback = new CopyFileCallback(CopyProgressChanged); try { AdvancedFileHandling.CopyFile(path, newpath, CopyFileOptions.None, callback); } catch (IOException) { //Assume the user cancelled return; } //Then delete the input File.Delete(path); }
public static void CopyFile(string source, string destination, CopyFileCallback callback) { CopyFile(new FileInfo(source), new FileInfo(destination), CopyFileOptions.None, callback); }
/// <summary> </summary> public static void copy_file_to(FileInfo source, FileInfo destination, CopyFileOptions options, CopyFileCallback callback) { copy_file_to(source, destination, options, callback, null); }
public static void MoveFile(FileInfo source, FileInfo destination, CopyFileOptions options, CopyFileCallback callback, object state) { CopyFile(source, destination, options, callback, state); source.Delete(); }
/// <summary> /// Move file from source to destination with progress callback /// </summary> /// <param name="source">source file</param> /// <param name="destination">destination file</param> /// <param name="overwrite">true if destination must be overwritten if exists</param> /// <param name="callback">progress callback method to be called</param> public static void MoveFile(string source, string destination, bool overwrite, CopyFileCallback callback) { CopyOrMoveFile(source, destination, overwrite, true, callback); }
public static void CopyFile(string source, string destination, CopyFileOptions options, CopyFileCallback callback) { CopyFile(source, destination, options, callback, null); }
/// <summary> /// Copy file from source to destination with progress callback /// </summary> /// <param name="source">source file</param> /// <param name="destination">destination file</param> /// <param name="overwrite">true if destination must be overwritten if exists</param> /// <param name="callback">progress callback method to be called</param> public static void CopyFile(string source, string destination, bool overwrite, CopyFileCallback callback) { CopyOrMoveFile(source, destination, overwrite, false, callback); }
/// <summary> /// Copy or move file from source to destination with progress callback /// </summary> /// <param name="source">source file</param> /// <param name="destination">destination file</param> /// <param name="overwrite">true if destination must be overwritten if exists</param> /// <param name="move">true if move, false=copy</param> /// <param name="callback">progress callback method to be called</param> public static void CopyOrMoveFile(string source, string destination, bool overwrite, bool move, CopyFileCallback callback) { if (source == null) { throw new ArgumentNullException("source"); } if (destination == null) { throw new ArgumentNullException("destination"); } new FileIOPermission(FileIOPermissionAccess.Read, source).Demand(); new FileIOPermission(FileIOPermissionAccess.Write, destination).Demand(); CopyProgressData progData = null; NativeMethods.CopyProgressRoutine cpr = null; if (callback != null) { progData = new CopyProgressData(source, destination, callback); cpr = progData.CallbackHandler; } bool cancel = false; if (Environment.OSVersion.Platform == PlatformID.Win32NT) { try { bool ret = true; if (move) { NativeMethods.MoveFileExFlags fm = NativeMethods.MoveFileExFlags.MOVEFILE_COPY_ALLOWED; if (overwrite) { fm |= NativeMethods.MoveFileExFlags.MOVEFILE_REPLACE_EXISTING; } ret = NativeMethods.MoveFileWithProgress(source, destination, cpr, IntPtr.Zero, (int)fm); } else { NativeMethods.CopyFileExFlags f = NativeMethods.CopyFileExFlags.COPY_FILE_ALLOW_DECRYPTED_DESTINATION; if (!overwrite) { f |= NativeMethods.CopyFileExFlags.COPY_FILE_FAIL_IF_EXISTS; } ret = NativeMethods.CopyFileEx(source, destination, cpr, IntPtr.Zero, ref cancel, (int)f); } if (progData != null && progData.Exception != null) { Utils.Rethrow(progData.Exception); } if (!ret) { var w = new Win32Exception(); if (w.NativeErrorCode == 2) { throw new FileNotFoundException(w.Message, source); } throw new IOException(w.Message, w); } return; } catch (MissingMethodException) { } catch (SecurityException) { } } // The old fashioned way if (source != destination) { if (overwrite) { var fi = new FileInfo(destination); if (fi.Exists && (fi.Attributes & (FileAttributes.Hidden | FileAttributes.ReadOnly | FileAttributes.System)) != 0) { fi.Attributes &= ~(FileAttributes.Hidden | FileAttributes.ReadOnly | FileAttributes.System); } fi.Delete(); } if (move) { File.Move(source, destination); } else { File.Copy(source, destination, overwrite); } return; } }
/// <summary> /// Copy or move file from source to destination with progress callback /// </summary> /// <param name="source">source file</param> /// <param name="destination">destination file</param> /// <param name="overwrite">true if destination must be overwritten if exists</param> /// <param name="move">true if move, false=copy</param> /// <param name="callback">progress callback method to be called</param> public static void CopyOrMoveFile(string source, string destination, bool overwrite, bool move, CopyFileCallback callback) { if (source == null) throw new ArgumentNullException("source"); if (destination == null) throw new ArgumentNullException("destination"); new FileIOPermission(FileIOPermissionAccess.Read, source).Demand(); new FileIOPermission(FileIOPermissionAccess.Write, destination).Demand(); CopyProgressData progData = null; NativeMethods.CopyProgressRoutine cpr = null; if (callback != null) { progData = new CopyProgressData(source, destination, callback); cpr = progData.CallbackHandler; } bool cancel = false; if (Environment.OSVersion.Platform == PlatformID.Win32NT) { try { bool ret = true; if (move) { NativeMethods.MoveFileExFlags fm = NativeMethods.MoveFileExFlags.MOVEFILE_COPY_ALLOWED; if (overwrite) fm |= NativeMethods.MoveFileExFlags.MOVEFILE_REPLACE_EXISTING; ret = NativeMethods.MoveFileWithProgress(source, destination, cpr, IntPtr.Zero, (int)fm); } else { NativeMethods.CopyFileExFlags f = NativeMethods.CopyFileExFlags.COPY_FILE_ALLOW_DECRYPTED_DESTINATION; if (!overwrite) f |= NativeMethods.CopyFileExFlags.COPY_FILE_FAIL_IF_EXISTS; ret = NativeMethods.CopyFileEx(source, destination, cpr, IntPtr.Zero, ref cancel, (int)f); } if (progData != null && progData.Exception != null) Utils.Rethrow(progData.Exception); if (!ret) { var w = new Win32Exception(); if (w.NativeErrorCode == 2) throw new FileNotFoundException(w.Message, source); throw new IOException(w.Message, w); } return; } catch (MissingMethodException) { } catch (SecurityException) { } } // The old fashioned way if (source != destination) { if (overwrite) { var fi = new FileInfo(destination); if (fi.Exists && (fi.Attributes & (FileAttributes.Hidden | FileAttributes.ReadOnly | FileAttributes.System))!=0) fi.Attributes&=~(FileAttributes.Hidden | FileAttributes.ReadOnly | FileAttributes.System); fi.Delete(); } if (move) File.Move(source, destination); else File.Copy(source, destination, overwrite); return; } }
/// <summary> </summary> public static void copy_file_to(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)) { String custom_message = "\r\n source:" + source.FullName + "\r\n destination:" + destination.FullName; logger.writelog("file handling failure:" + custom_message); throw new IOException(new Win32Exception().Message.Insert(0, custom_message)); } }
/// <summary> /// 拷贝目录下所有文件 /// </summary> /// <param name="strSrcDir"></param> /// <param name="strDestDir"></param> /// <param name="lstFile"></param> /// <param name="lstFilter"></param> /// <param name="callback"></param> public void CopyDirectory(string strSrcDir, string strDestDir, ref List <string> lstFile, List <string> lstFilter = null, CopyFileCallback callback = null, bool toLower = false) { strSrcDir = strSrcDir.Replace("\\", "/"); strDestDir = strDestDir.Replace("\\", "/"); if (toLower) { strDestDir = strDestDir.ToLower(); strSrcDir = strSrcDir.ToLower(); } DirectoryInfo src = new DirectoryInfo(strSrcDir); if (!src.Exists) { return; } DirectoryInfo dest = new DirectoryInfo(strDestDir); if (!dest.Exists) { CreateDir(strDestDir); } if (strSrcDir.LastIndexOf('/') != strSrcDir.Length - 1) { strSrcDir += "/"; } DirectoryInfo dir = src as DirectoryInfo; //不是目录 if (dir == null) { return; } FileSystemInfo[] files = dir.GetFileSystemInfos(); for (int i = 0; i < files.Length; i++) { FileInfo file = files[i] as FileInfo; //是文件 if (file != null) { string strDestFileName = strDestDir + "/"; strDestFileName += file.Name; string strExt = Path.GetExtension(strDestFileName); if (lstFilter != null && lstFilter.Contains(strExt)) { continue; } file.CopyTo(strDestFileName.ToLower(), true); if (lstFile != null) { lstFile.Add(file.FullName); } if (callback != null) { callback(file.FullName); } } else { string strFullDirName = files[i].FullName; strFullDirName = strFullDirName.Replace("\\", "/"); DirectoryInfo srcDir = new DirectoryInfo(strSrcDir); strSrcDir = srcDir.FullName.Replace("\\", "/"); string strDir = strFullDirName.Replace(strSrcDir, ""); string strSubDir = strDestDir + "/"; strSubDir += strDir; if (toLower) { strFullDirName = strFullDirName.ToLower(); strSubDir = strSubDir.ToLower(); } CopyDirectory(strFullDirName, strSubDir, ref lstFile, lstFilter, callback, toLower); } } }