private static string FileSafeAction(String to, SystemSafe safe = SystemSafe.Override) { try { if (File.Exists(to)) { String toName, toExt; int Counter = 1; switch (safe) { case SystemSafe.KeepBoth: toName = Path.GetFileNameWithoutExtension(to); Boolean LastisBracket = toName.Substring(toName.Length - 1).Equals(")"); Boolean ContainRepaeat = int.TryParse(toName.Substring(toName.LastIndexOf('(')).Replace("(", "").Replace(")", ""), out int count); if (LastisBracket) { if (ContainRepaeat) { toName = toName.Substring(0, toName.LastIndexOf('(')); } } toExt = Path.GetExtension(to); while (File.Exists(to)) { to = $"{toName}({Counter++}){toExt}"; } break; case SystemSafe.Override: DeleteFile(to); break; case SystemSafe.Skip: to = null; break; } } return(to); } catch (Exception e) { ErrorLog.Write(e, MethodBase.GetCurrentMethod().Name); return(to); } }
/// <summary> /// <para>Move file</para> /// <para>Override</para> /// </summary> public static void MoveFile(String from, String to, SystemSafe safe = SystemSafe.Override) { try { if (File.Exists(from)) { to = FileSafeAction(to, safe); if (!String.IsNullOrEmpty(to)) { File.Move(from, to); } } } catch (Exception e) { ErrorLog.Write(e, MethodBase.GetCurrentMethod().Name); } }
/// <summary> /// <para>Copy everything in the directory</para> /// <para>Level All</para> /// </summary> public static void CopyDirectory(String from, String to, SystemSafe safe = SystemSafe.Override) { try { Directory.CreateDirectory(to); List <DirectoryInfo> folderList = GetFolders(from); foreach (DirectoryInfo folder in folderList) { CopyDirectory(folder.FullName, $@"{to}\{folder.Name}", safe); } List <FileInfo> fileList = GetFiles(from); foreach (FileInfo file in fileList) { CopyFile(file.FullName, $@"{to}\{file.Name}", safe); } } catch (Exception e) { ErrorLog.Write(e, MethodBase.GetCurrentMethod().Name); } }