Я могу предложить этот код:
static IEnumerable<FileSystemInfo> GetAllFilesAndDirectories(string path)
{
string currentDirectory = "";
string[] files = Directory.GetFiles( // skip empty subfolders
path, "*.*", SearchOption.AllDirectories);
foreach (string file in files)
{
if(currentDirectory != Path.GetDirectoryName(file))
{
// First time in this directory: return it
currentDirectory = Path.GetDirectoryName(file);
yield return new DirectoryInfo(currentDirectory);
}
yield return new FileInfo(file);
}
}
static void Main(string[] args)
{
foreach (FileSystemInfo info in GetAllFilesAndDirectories(@"c:\intel"))
{
Console.WriteLine("{0} ({1})", info.FullName, info.Attributes);
}
}
Это отобразит:
c:\intel\Logs (Directory)
c:\intel\Logs\IntelChipset.log (Archive)
c:\intel\Logs\IntelGFX.log (Archive)
c:\intel\Logs\IntelStor.log (Archive)
c:\intel\ExtremeGraphics\CUI\Resource (Directory)
c:\intel\ExtremeGraphics\CUI\Resource\igfxres.dll (Archive)
Если вы хотите List<>
, просто вызовите .ToList()
метод