static void Main(string[] args) { try { if (args.Length == 0) PrintUsage(); else { Mp3SyncSettings settings = new Mp3SyncSettings(); #region args if (args.Length > 1) { for (int i = 0; i < args.Length; ++i) { switch (args[i]) { case "-s": settings.Source = args[++i]; break; case "-d1": case "-d2": settings.Dests.Add(args[++i]); break; case "-dbin": settings.DestBin = args[++i]; break; case "ext": settings.StExt = args[++i]; break; case "auto": settings.AutoSync = args[++i]; break; default: throw new ArgumentException("Unknown option: " + args[i]); } } } else { if (File.Exists(args[0]) && (Path.GetExtension(args[0]) == ".xml")) { using (StreamReader sr = new StreamReader(args[0], true)) { XmlSerializer s = new XmlSerializer(typeof(Mp3SyncSettings)); settings = (Mp3SyncSettings)(s.Deserialize(sr)); } } else { PersistSettings(new Mp3SyncSettings(true)); throw new Exception("Could not process the xml input " + args[0] + " see the generated Mp3SyncInputs.xml input file"); } } PersistSettings(settings); foreach(string dir in settings.Dests) if (!Directory.Exists(dir)) throw new ArgumentException("can't find dest dir: " + dir); if (!Directory.Exists(settings.Source)) throw new ArgumentException("can't find source dir: " + settings.Source); if ((settings.DestBin != null) && (!Directory.Exists(settings.DestBin))) throw new ArgumentException("can't find destination Bin dir: " + settings.DestBin); #endregion Mp3Sync.Mp3Synchronizer.synchronize(settings); Console.WriteLine("Process over - Press any key"); Console.ReadKey(); } } catch (Exception e) { Console.WriteLine("\n-- Error --"); Console.WriteLine(e.Message); #if DEBUG Console.WriteLine(e.StackTrace); #endif Console.WriteLine("\nProcess over - Press any key"); Console.ReadKey(); } }
//Add Target2 + mode public static void synchronize(Mp3SyncSettings inputs) { try { long iCountSnip = 0; Dictionary<string, FileInfo> srcMap = new Dictionary<string, FileInfo>(); long srcCapacity = 0; Console.WriteLine(Resources.Step1); Console.WriteLine(); srcCapacity += SearchForInterestFiles(new DirectoryInfo(inputs.Source), srcMap, inputs.StExt); Console.WriteLine(String.Format(Resources.srcCount, srcMap.Count)); Console.WriteLine(); Dictionary<string, FileInfo> dstMap = new Dictionary<string, FileInfo>(); long destCapacity = 0; foreach (string starget in inputs.Dests) { DirectoryInfo target = new DirectoryInfo(starget); DriveInfo dTargetInfo = new DriveInfo(target.FullName.Substring(0, 1)); destCapacity += dTargetInfo.AvailableFreeSpace; destCapacity += SearchForInterestFiles(target, dstMap, inputs.StExt); destCapacity -= 10000000; //10Mo kept free per unit } Console.WriteLine(String.Format(Resources.dstCount, dstMap.Count)); Console.WriteLine(); Console.WriteLine(String.Format(Resources.srcWeight, srcCapacity / 1000000)); Console.WriteLine(String.Format(Resources.dstCapacity, destCapacity / 1000000)); Console.WriteLine(); long free = destCapacity - srcCapacity; Console.WriteLine(String.Format(Resources.freeSpace, ((free > 0) ? "+" : ""), free / 1000000)); if (free < 0) Console.WriteLine(Resources.warnLackPlace); Console.WriteLine(Resources.Step2); if (inputs.DestBin != null) Console.WriteLine(String.Format(Resources.extraFilesDest, inputs.DestBin)); else Console.WriteLine(Resources.extradel); //Snip extra dest files foreach (KeyValuePair<string, FileInfo> kvp in dstMap) { if (!srcMap.ContainsKey(kvp.Key)) { if (inputs.DestBin != null) { string destName = Path.Combine(inputs.DestBin, kvp.Value.Directory.Name); if (!Directory.Exists(destName)) Directory.CreateDirectory(destName); destName = Path.Combine(destName, kvp.Value.Name); if (!File.Exists(destName)) kvp.Value.MoveTo(destName); else kvp.Value.Delete(); } else { try { kvp.Value.Delete(); } catch (Exception e) { throw new Exception(String.Format(Resources.errorNoDelete, kvp.Value.FullName, e.ToString())); } } ++iCountSnip; } } Console.WriteLine(String.Format(Resources.removedFiles, iCountSnip)); //Snip empty directories foreach (string target in inputs.Dests) SnipVoidDir(new DirectoryInfo(target)); Console.WriteLine(Resources.Step3); //Add new files in dest long count = 0; foreach (string target in inputs.Dests) count += AddInDest(new DirectoryInfo(target), srcMap, dstMap, inputs.AutoSync); //end of Add Console.WriteLine(String.Format(Resources.copiedFiles, count)); if (srcMap.Count > 0) { Console.WriteLine(Resources.errorNoPlace); foreach (KeyValuePair<string, FileInfo> kvp in srcMap) { Console.WriteLine(Path.Combine(Path.GetDirectoryName(kvp.Value.FullName), kvp.Value.Name)); } } else Console.WriteLine(Resources.synchroOK); return; } catch (Exception) { throw; } }
private static void PersistSettings(Mp3SyncSettings settings) { if (!File.Exists(@"Mp3SyncInputs.xml")) { //Persist curr settings XmlSerializer s = new XmlSerializer(typeof(Mp3SyncSettings)); System.IO.TextWriter xw = new System.IO.StreamWriter(@"Mp3SyncInputs.xml", false, System.Text.Encoding.UTF8); s.Serialize(xw, settings); xw.Close(); } }