Obtención de archivos por fecha de creación in.NET


Tengo una carpeta que contiene muchos archivos. ¿Hay alguna manera fácil de obtener los nombres de archivo en el directorio ordenados por su fecha/hora de creación?

Si utilizo Directory.GetFiles(), devuelve los archivos ordenados por su nombre de archivo.

 73
Author: Senseful, 2011-01-22

6 answers

Esto podría funcionar para usted.

using System.Linq;

DirectoryInfo info = new DirectoryInfo("PATH_TO_DIRECTORY_HERE");
FileInfo[] files = info.GetFiles().OrderBy(p => p.CreationTime).ToArray();
foreach (FileInfo file in files)
{
    // DO Something...
}
 154
Author: George Taskos,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-02-22 15:27:11

Puede usar Linq

var files = Directory.GetFiles(@"C:\", "*").OrderByDescending(d => new FileInfo(d).CreationTime);
 37
Author: Sev,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-01-22 02:31:59

Si no desea utilizar LINQ

// Get the files
DirectoryInfo info = new DirectoryInfo("path/to/files"));
FileInfo[] files = info.GetFiles();

// Sort by creation-time descending 
Array.Sort(files, delegate(FileInfo f1, FileInfo f2)
{
    return f2.CreationTime.CompareTo(f1.CreationTime);
});
 7
Author: Henrik,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-05-13 09:27:10

Esto devuelve la última fecha de modificación y su antigüedad.

DateTime.Now.Subtract(System.IO.File.GetLastWriteTime(FilePathwithName).Date)
 4
Author: BMG,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-09-24 11:25:57

@jing:"La solución DirectoryInfo es mucho más rápida que esto (especialmente para la ruta de red)"

No puedo confirmar esto. Parece como si el Directorio.GetFiles activa un sistema de archivos o caché de red. La primera solicitud toma un tiempo, pero las siguientes solicitudes son mucho más rápidas, incluso si se agregaron nuevos archivos. En mi prueba hice un Directorio.getfiles y una información.GetFiles con los mismos patrones y ambos corren igual

GetFiles  done 437834 in00:00:20.4812480
process files  done 437834 in00:00:00.9300573
GetFiles by Dirinfo(2)  done 437834 in00:00:20.7412646
 1
Author: Passer,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-11-22 11:27:46
            DirectoryInfo dirinfo = new DirectoryInfo(strMainPath);
            String[] exts = new string[] { "*.jpeg", "*.jpg", "*.gif", "*.tiff", "*.bmp","*.png", "*.JPEG", "*.JPG", "*.GIF", "*.TIFF", "*.BMP","*.PNG" };
            ArrayList files = new ArrayList();
            foreach (string ext in exts)
                files.AddRange(dirinfo.GetFiles(ext).OrderBy(x => x.CreationTime).ToArray());
 0
Author: Ata Hoseini,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-04-07 05:24:01