public static void AddNewJob(string name, string source, string destination) { if (File.Exists(SETTINGS_PATH + "Jobs.xml")) { List<Job> jobs = GetAllJobs(); jobs.Add(new Job(name, source, destination)); using (Stream fs = new FileStream(SETTINGS_PATH + "Jobs.xml", FileMode.Open, FileAccess.Write, FileShare.None)) { XmlSerializer xmlFormat = new XmlSerializer(typeof(List<Job>)); xmlFormat.Serialize(fs, jobs); } } else { using (Stream fs = new FileStream(SETTINGS_PATH + "Jobs.xml", FileMode.Create, FileAccess.Write, FileShare.None)) { List<Job> jobs = new List<Job>(); Job newJob = new Job(name, source, destination); jobs.Add(newJob); XmlSerializer xmlFormat = new XmlSerializer(typeof(List<Job>)); xmlFormat.Serialize(fs, jobs); } } MainWindow.mainWindow.JobListUpdate(); }
private async Task CopyAllFilesAsync(Job job) { foreach (string dir in Directory.GetDirectories(job.Source, "*", SearchOption.AllDirectories)) { Directory.CreateDirectory(dir.Replace(job.Source, job.Where)); foreach (string filename in Directory.EnumerateFiles(dir)) { using (FileStream srcStream = File.Open(filename, FileMode.Open)) { using (FileStream stream = File.Create(filename.Replace(job.Source, job.Where))) { await srcStream.CopyToAsync(stream); } } } } foreach (string filename in Directory.EnumerateFiles(job.Source)) { using (FileStream srcStream = File.Open(filename, FileMode.Open)) { using (FileStream stream = File.Create(job.Where + filename.Substring(filename.LastIndexOf('\\')))) { await srcStream.CopyToAsync(stream); } } } }
private bool CheckPath(Job job) { if(Directory.Exists(job.Source) && Directory.Exists(job.Where)) { return true; } return false; }
private bool CheckIsDirEmpty(Job job) { if(Directory.GetDirectories(job.Where).Length + Directory.GetFiles(job.Where).Length == 0) { return true; } return false; }
public FilesWatcher(Job job) { _source = job.Source; _destination = job.Where; }