简体   繁体   中英

How do i use DirectoryInfo and FileInfo to get all files and add the sorted files to a List?

I want to sort the file make from them List to use them later in a method:

dirinfo = new DirectoryInfo(radarImagesDirectory);
var ordered = dirinfo.EnumerateFiles("*.gif")
         .OrderByDescending(f => f.LastWriteTime);
FileInfo newest = ordered.FirstOrDefault();
FileInfo oldest = ordered.LastOrDefault();

I want to use the sorted files in this method:

private List<Bitmap> ConvertTo24(List<string> ListinputFileName)
        {
            Bitmap bmpIn = null;
            for (int i = 0; i < ListinputFileName.Count; i++)
            {
                bmpIn = (Bitmap)Bitmap.FromFile(ListinputFileName[i]);
            }
            Bitmap converted = new Bitmap(bmpIn.Width, bmpIn.Height, PixelFormat.Format24bppRgb);
            using (Graphics g = Graphics.FromImage(converted))
            {
                g.PageUnit = GraphicsUnit.Pixel;
                g.DrawImageUnscaled(bmpIn, 0, 0);
            }
            ConvertedBitmaps.Add(converted);
            return ConvertedBitmaps;
        }

A possibility to get the list of giffiles and add them to the testfiles (ordering has to be done afterwards) would be this:

List<String> giffiles = Directory.GetFiles(radarImagesDirectory, "*.gif")
                                     .Select(path => Path.GetFileName(path))
                                     .ToList();

testfiles.AddRange(giffiles);
testfiles = testfiles.OrderBy(q => q).ToList();

The first line gets all giffiles into a list, the 2nd line adds the list to the preexisting list and the 3rd line orders the list.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM