Код ниже возвращает только самое последнее значение в цикле.Что мне нужно сделать, чтобы показать все значения, которые перебираются?Причина, по которой я использую этот метод вместо SearchOption.AllDirectory
, заключается в том, что существует путь к папке, к которому я не могу получить доступ.Я попытался использовать UnauthorizedAccessException
с try
и catch
, но это возвращает пустое значение, потому что оно продолжает завершать цикл.
public void Main()
{
string path = @"drive"; // TODO
ApplyAllFiles(path, ProcessFile);
}
public void ProcessFile(string path)
{
/* ... */
}
public void ApplyAllFiles(string folder, Action<string> fileAction)
{
System.Collections.ArrayList FileList = new System.Collections.ArrayList();
List<string> logger = new List<string>();
DateTime DateFilter = DateTime.Now.AddMonths(-6);
foreach (string file in Directory.GetDirectories(folder))
{
fileAction(file);
}
foreach (string subDir in Directory.GetDirectories(folder))
{
string rootfolder = "root folder";
if (subDir.Contains(rootfolder))
{
FileList.Add(subDir);
Dts.Variables["User::objDirectoryList"].Value = FileList;
//MessageBox.Show(subDir);
}
try
{
ApplyAllFiles(subDir, fileAction);
}
catch (UnauthorizedAccessException e)
{
logger.Add(e.Message);
}
catch (System.IO.DirectoryNotFoundException e)
{
logger.Add(e.Message);
}
}
}