|
Este artículo proviene de un motor de traducción automática. Mueva el puntero sobre las frases del artículo para ver el texto original. Más información.
|
Traducción
Original
|
Cómo: Buscar el número total de bytes en un conjunto de carpetas (LINQ)
class QuerySize { public static void Main() { string startFolder = @"c:\program files\Microsoft Visual Studio 9.0\VC#"; // Take a snapshot of the file system. // This method assumes that the application has discovery permissions // for all folders under the specified path. IEnumerable<string> fileList = System.IO.Directory.GetFiles(startFolder, "*.*", System.IO.SearchOption.AllDirectories); var fileQuery = from file in fileList select GetFileLength(file); // Cache the results to avoid multiple trips to the file system. long[] fileLengths = fileQuery.ToArray(); // Return the size of the largest file long largestFile = fileLengths.Max(); // Return the total number of bytes in all the files under the specified folder. long totalBytes = fileLengths.Sum(); Console.WriteLine("There are {0} bytes in {1} files under {2}", totalBytes, fileList.Count(), startFolder); Console.WriteLine("The largest files is {0} bytes.", largestFile); // Keep the console window open in debug mode. Console.WriteLine("Press any key to exit."); Console.ReadKey(); } // This method is used to swallow the possible exception // that can be raised when accessing the System.IO.FileInfo.Length property. static long GetFileLength(string filename) { long retval; try { System.IO.FileInfo fi = new System.IO.FileInfo(filename); retval = fi.Length; } catch (System.IO.FileNotFoundException) { // If a file is no longer present, // just add zero bytes to the total. retval = 0; } return retval; } }
-
Cree un proyecto de Visual Studio para la versión 3.5 de .NET Framework. De manera predeterminada, el proyecto incluye una referencia a System.Core.dll y una directiva using (C#) o una instrucción Imports (Visual Basic) para el espacio de nombres System.Linq. En los proyectos de C#, agregue una directiva using para el espacio de nombres System.IO. -
Copie este código en el proyecto. -
Presione F5 para compilar y ejecutar el programa. -
Presione cualquier tecla para salir de la ventana de consola.