/// <summary> /// 移动或复制一个或多个文件到指定路径下 /// </summary> /// <param name="flag">操作类型,是移动操作还是复制操作</param> /// <param name="sourceFileName">要移动或复制的文件名,如果是多个文件,文件名之间以字符串结尾符'\0'隔开</param> /// <param name="destinationFileName">移动到的目的位置</param> /// <param name="showDialog">指示是否显示确认对话框,true-显示确认对话框,false-不显示确认对话框</param> /// <param name="showProgress">指示是否显示进度对话框</param> /// <param name="autoRename">指示当文件名重复时,是否自动为新文件加上后缀名</param> /// <param name="errorMsg">反馈错误消息的字符串</param> /// <returns>返回移动操作是否成功的标识,成功返回0,失败返回错误代码</returns> private static int ToMoveOrCopy(wFunc flag, string sourceFileName, string destinationFileName, bool showDialog, bool showProgress, bool autoRename, ref string errorMsg) { SHFILEOPSTRUCT lpFileOp = new SHFILEOPSTRUCT(); lpFileOp.wFunc = flag; lpFileOp.pFrom = sourceFileName + "\0"; //将文件名以结尾字符"\0\0"结束 lpFileOp.pTo = destinationFileName + "\0\0"; lpFileOp.fFlags = FILEOP_FLAGS.FOF_NOERRORUI; lpFileOp.fFlags |= FILEOP_FLAGS.FOF_NOCONFIRMMKDIR; //指定在需要时可以直接创建路径 if (!showDialog) { lpFileOp.fFlags |= FILEOP_FLAGS.FOF_NOCONFIRMATION; //设定不显示提示对话框 } if (!showProgress) { lpFileOp.fFlags |= FILEOP_FLAGS.FOF_SILENT; //设定不显示进度对话框 } if (autoRename) { lpFileOp.fFlags |= FILEOP_FLAGS.FOF_RENAMEONCOLLISION; //自动为重名文件添加名称后缀 } lpFileOp.fAnyOperationsAborted = true; int n = SHFileOperation(ref lpFileOp); if (n == 0) { return(0); } string tmp = GetErrorString(n); errorMsg = string.Format("{0}({1})", tmp, sourceFileName); return(n); }
/// <summary> /// 移动或复制一个或多个文件到指定路径下 /// </summary> /// <param name="flag">操作类型,是移动操作还是复制操作</param> /// <param name="sourceFileName">要移动或复制的文件名,如果是多个文件,文件名之间以字符串结尾符'\0'隔开</param> /// <param name="destinationFileName">移动到的目的位置</param> /// <param name="showDialog">指示是否显示确认对话框,true-显示确认对话框,false-不显示确认对话框</param> /// <param name="showProgress">指示是否显示进度对话框</param> /// <param name="autoRename">指示当文件名重复时,是否自动为新文件加上后缀名</param> /// <param name="errorMsg">反馈错误消息的字符串</param> /// <returns>返回移动操作是否成功的标识,成功返回0,失败返回错误代码</returns> private static int ToMoveOrCopy(wFunc flag, string sourceFileName, string destinationFileName, bool showDialog, bool showProgress, bool autoRename, ref string errorMsg) { SHFILEOPSTRUCT lpFileOp = new SHFILEOPSTRUCT(); lpFileOp.wFunc = flag; lpFileOp.pFrom = sourceFileName + "\0"; //将文件名以结尾字符"\0\0"结束 lpFileOp.pTo = destinationFileName + "\0\0"; lpFileOp.fFlags = FILEOP_FLAGS.FOF_NOERRORUI; lpFileOp.fFlags |= FILEOP_FLAGS.FOF_NOCONFIRMMKDIR; //指定在需要时可以直接创建路径 if (!showDialog) lpFileOp.fFlags |= FILEOP_FLAGS.FOF_NOCONFIRMATION; //设定不显示提示对话框 if (!showProgress) lpFileOp.fFlags |= FILEOP_FLAGS.FOF_SILENT; //设定不显示进度对话框 if (autoRename) lpFileOp.fFlags |= FILEOP_FLAGS.FOF_RENAMEONCOLLISION; //自动为重名文件添加名称后缀 lpFileOp.fAnyOperationsAborted = true; int n = SHFileOperation(ref lpFileOp); if (n == 0) return 0; string tmp = GetErrorString(n); errorMsg = string.Format("{0}({1})", tmp, sourceFileName); return n; }