using System; using System.IO; class DirectoryLister { public static void Main() { try { DirectoryInfo dir = new DirectoryInfo( "c:\\" ); Console.WriteLine(); Console.WriteLine(); Console.WriteLine("A continuación se muestra un listado del directorio: {0}", Path.GetFullPath(dir.ToString())); Console.WriteLine(); Console.WriteLine("{0, -25} {1,-12:N0} {2, -12} {3,-20:g}", "Nombre", ("Tamaño").PadLeft(12), "Tipo", "Hora de creación"); Console.WriteLine("----------------------------------------------------------------------------"); foreach(FileSystemInfo fsi in dir.GetFileSystemInfos()) { try { DateTime creationTime = fsi.CreationTime; int subLength = 25; if (fsi is FileInfo) { FileInfo f = (FileInfo)fsi; //this if statement simply ensures that we do not shorten the name of the file too much! if (f.Name.Length < subLength) subLength = f.Name.Length; String name = f.Name.Substring(0, subLength); long size = f.Length; // format the output to the screen Console.WriteLine("{0, -25} {1,-12:N0} {2, -12} {3,-20:g}", name, (size + " KB").PadLeft(12), "Archivo", creationTime); } else //it must be a directory { DirectoryInfo d = (DirectoryInfo)fsi; //this if statement simply ensures that we do not shorten the name of the file too much! if (d.Name.Length < subLength) subLength = d.Name.Length; String dirName = d.Name.Substring(0, subLength); // format the output to the screen Console.WriteLine("{0, -25} {1,-12:N0} {2, -12} {3,-20:g}", dirName, ("").PadLeft(12), "Directorio", creationTime); } } catch (Exception) { } //ignore the error, and try the next item... } } catch (Exception e) { Console.WriteLine("--- PROCESO FINALIZADO ---"); Console.WriteLine(e.Message); } Console.ReadLine(); } }