Как переместить файлы из родительской исходной папки в целевую папку в C#?
Я хочу переместить файлы из родительской исходной папки в целевую папку в c#. В родительской исходной папке будет Родительская подпапка и в ней несколько подпапок, и если в этих подпапках существует какой-либо файл, то эти файлы должны быть заархивированы с той же структурой, что и в родительской исходной папке.
Я попытался вызвать рекурсивный метод, но архивирование работает только для одной родительской подпапки.
Что я уже пробовал:
public void TEST(string source,string dest) { string tempsource = string.Empty; string tempdest = string.Empty; if (string.IsNullOrEmpty(dest)==false) { DirectoryInfo dirInfo = new DirectoryInfo(dest); if (dirInfo.Exists == false) Directory.CreateDirectory(dest); DirectoryInfo dir = new DirectoryInfo(source); DirectoryInfo[] dirs = dir.GetDirectories(); int archivalagefield = 0; foreach (DirectoryInfo subdir in dirs) { tempsource = Path.Combine(source, subdir.Name); tempdest = Path.Combine(dest, subdir.Name); if (!Directory.Exists(tempdest)) try { //Directory.Move(subdir.FullName, temppath); Directory.CreateDirectory(tempdest); string[] files = Directory.GetFiles(tempsource); GetArchiveLogDate GA = new GetArchiveLogDate(); DataSet dsGetArchiveAgeDate = GA.FetchMaxArchiveDateValue(); string ArchivalAge = dsGetArchiveAgeDate.Tables[1].Rows[0]["PARAMVALUE"].ToString().Trim(); if (dsGetArchiveAgeDate.Tables.Count > 0) { if (dsGetArchiveAgeDate.Tables[1].Rows.Count > 0) { archivalagefield = int.Parse(ArchivalAge) * -1; } } foreach (string file in files) { try { DateTime FromDateTime = DateTime.Now.AddDays(ArchivalAgeField); DateTime FromDate = Convert.ToDateTime(FromDateTime.ToString("yyyy/MM/dd")); DateTime dtFileCreatedDate = File.GetCreationTime(file); if (dtFileCreatedDate <= FromDate) // Only if the CreatedDate is less than FromDate, directory needs to be created and file needs to be moved { string name = Path.GetFileName(file); string destFile = Path.Combine(tempdest, name); if (name != "file") File.Move(file, destFile); } } catch { } } } catch { } } } tempsource = Path.Combine(tempsource, appendedfolder); tempdest = Path.Combine(tempdest, appendedfolder); TEST(tempsource, tempdest); }