/// <summary> /// Execute the move operation using the linux mv -r sintax, where the destination will be /// the parent directory of the moved one. /// </summary> /// <param name="source"></param> /// <param name="destination"></param> /// <param name="showUi"></param> public static void dirMv(string source, string destination, bool showUi) { source = CSharp.cleanPath(source); destination = CSharp.cleanPath(destination); // cleanPath(string path) if (CSharp.stlogMethod == LogMethod.LOGGER) { log.Info("mv -r \"" + source + "\" \"" + destination + "\""); } Console.WriteLine("mv -r \"" + source + "\" \"" + destination + "\""); char[] charSeparators = new char[] { '\\' }; string[] dirs = source.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries); string lastElement = dirs[dirs.Length - 1]; destination = destination + "\\" + lastElement + "\\"; if (showUi) { // do the move FileSystem.MoveDirectory(source, destination, UIOption.AllDialogs, UICancelOption.ThrowException); } else { Directory.Move(source, destination); } }
public static Err safeMove(string sourceDirName, string destDirName) { sourceDirName = CSharp.cleanPath(sourceDirName); destDirName = CSharp.cleanPath(destDirName); LogMethod logMethod = LogMethod.NONE; return(safeMove(sourceDirName, destDirName, logMethod)); }
public static Err safeMove(string sourceDirName, string destDirName, LogMethod logMethod) { sourceDirName = CSharp.cleanPath(sourceDirName); destDirName = CSharp.cleanPath(destDirName); try { Directory.Move(sourceDirName, destDirName); return(Err.SUCCESS); } catch (PathTooLongException ex) { if (logMethod == LogMethod.LOGGER) { log.Warn("The specified path, file name, or both exceed the system-defined maximum length."); log.Warn("** Message:" + ex.Message + ", StackTrace:" + ex.StackTrace); } else if (logMethod == LogMethod.CONSOLE) { Console.WriteLine("The specified path, file name, or both exceed the system-defined maximum length."); Console.WriteLine("** Message:" + ex.Message + ", StackTrace:" + ex.StackTrace); } return(Err.PATH_TOO_LONG); } catch (DirectoryNotFoundException ex) { if (logMethod == LogMethod.LOGGER) { log.Warn("** Message:" + ex.Message + ", StackTrace:" + ex.StackTrace); log.Warn("The path specified by sourceDirName is invalid (for example, it is on an unmapped drive)."); } else if (logMethod == LogMethod.CONSOLE) { Console.WriteLine("** Message:" + ex.Message + ", StackTrace:" + ex.StackTrace); Console.WriteLine("The path specified by sourceDirName is invalid (for example, it is on an unmapped drive)."); } return(Err.DIRECTORY_NOT_FOUND); } catch (IOException ex) { if (logMethod == LogMethod.LOGGER) { log.Warn("An attempt was made to move a directory to a different volume. - or - " + "destDirName already exists. See the Note in the Remarks section. - or - " + "The sourceDirName and destDirName parameters refer to the same file or directory. - or - " + "The directory or a file within it is being used by another process."); log.Warn("** Message:" + ex.Message + ", StackTrace:" + ex.StackTrace); } else if (logMethod == LogMethod.CONSOLE) { Console.WriteLine("An attempt was made to move a directory to a different volume. - or - " + "destDirName already exists. See the Note in the Remarks section. - or - " + "The sourceDirName and destDirName parameters refer to the same file or directory. - or - " + "The directory or a file within it is being used by another process."); Console.WriteLine("** Message:" + ex.Message + ", StackTrace:" + ex.StackTrace); } return(Err.IO_EXCEPTION); } catch (UnauthorizedAccessException ex) { if (logMethod == LogMethod.LOGGER) { log.Warn("The caller does not have the required permission."); log.Warn("** Message:" + ex.Message + ", StackTrace:" + ex.StackTrace); } else if (logMethod == LogMethod.CONSOLE) { Console.WriteLine("The caller does not have the required permission."); Console.WriteLine("** Message:" + ex.Message + ", StackTrace:" + ex.StackTrace); } return(Err.UNAUTHORIZED_ACCESS); } catch (ArgumentNullException ex) { if (logMethod == LogMethod.LOGGER) { log.Warn("sourceDirName or destDirName is null."); log.Warn("** Message:" + ex.Message + ", StackTrace:" + ex.StackTrace); } else if (logMethod == LogMethod.CONSOLE) { Console.WriteLine("sourceDirName or destDirName is null."); Console.WriteLine("** Message:" + ex.Message + ", StackTrace:" + ex.StackTrace); } return(Err.ARGUMENT_NULL); } catch (ArgumentException ex) { if (logMethod == LogMethod.LOGGER) { log.Warn("sourceDirName or destDirName is a zero-length string, contains only white space, " + "or contains one or more invalid characters. You can query for invalid characters with " + " the GetInvalidPathChars() method."); log.Warn("** Message:" + ex.Message + ", StackTrace:" + ex.StackTrace); } else if (logMethod == LogMethod.CONSOLE) { Console.WriteLine("sourceDirName or destDirName is a zero-length string, contains only white space, " + "or contains one or more invalid characters. You can query for invalid characters with " + " the GetInvalidPathChars() method."); Console.WriteLine("** Message:" + ex.Message + ", StackTrace:" + ex.StackTrace); } return(Err.ARGUMENT_EXCEPTION); } catch (Exception ex) { if (logMethod == LogMethod.LOGGER) { log.Warn("** Message:" + ex.Message + ", StackTrace:" + ex.StackTrace); } else if (logMethod == LogMethod.CONSOLE) { Console.WriteLine("** Message:" + ex.Message + ", StackTrace:" + ex.StackTrace); } } return(Err.ERR_UNKNOWN); }