Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. get files from directory

get files from directory

Scheduled Pinned Locked Moved C#
question
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hamid_m
    wrote on last edited by
    #1

    get files from specif directory that sort by filename (DESC) ?

    C B 2 Replies Last reply
    0
    • H hamid_m

      get files from specif directory that sort by filename (DESC) ?

      C Offline
      C Offline
      ChrisKo 0
      wrote on last edited by
      #2

      DirectoryInfo dir = new DirectoryInfo(@"c:\");
      FileInfo[] files = dir.GetFiles();

      Array.Reverse(files);

      foreach(FileInfo file in files)
      {
      Console.WriteLine(file.Name);
      }

      Console.ReadLine();

      L 1 Reply Last reply
      0
      • H hamid_m

        get files from specif directory that sort by filename (DESC) ?

        B Offline
        B Offline
        balaji baskar
        wrote on last edited by
        #3

        Use the following line of code to get list of files. string[] files = Directory.GetFiles(@"C:\docs"); now iterate in your own fashion to descending order.

        Balaji

        1 Reply Last reply
        0
        • C ChrisKo 0

          DirectoryInfo dir = new DirectoryInfo(@"c:\");
          FileInfo[] files = dir.GetFiles();

          Array.Reverse(files);

          foreach(FileInfo file in files)
          {
          Console.WriteLine(file.Name);
          }

          Console.ReadLine();

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          Hi, AFAIK the order of the files returned by GetFiles() depends on the underlying file system; NTFS would sort alphabetically, FAT/FAT32 would not. :)

          Luc Pattyn [Forum Guidelines] [My Articles]


          this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google


          C 1 Reply Last reply
          0
          • L Luc Pattyn

            Hi, AFAIK the order of the files returned by GetFiles() depends on the underlying file system; NTFS would sort alphabetically, FAT/FAT32 would not. :)

            Luc Pattyn [Forum Guidelines] [My Articles]


            this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google


            C Offline
            C Offline
            ChrisKo 0
            wrote on last edited by
            #5

            I didn't realize that FAT/FAT32 weren't ordered. My bad. Implement a IComparer class then. You can change the compare method to sort however you want, by date if you wanted. Maybe even pass in an Enum of sort options.

            using System;
            using System.Collections;
            using System.IO;

            public class CompareFileInfo : IComparer
            {
            int IComparer.Compare(object first, object second)
            {
            FileInfo file1 = (FileInfo)first;
            FileInfo file2 = (FileInfo)second;

            	return string.Compare(file1.Name, file2.Name);
            
            }
            

            }

            public class MyClass
            {
            public static void Main()
            {
            DirectoryInfo dir = new DirectoryInfo(@"c:\");
            FileInfo[] files = dir.GetFiles();
            Array.Sort(files, new CompareFileInfo());
            Array.Reverse(files);
            foreach(FileInfo file in files)
            {
            Console.WriteLine(file.Name);
            }
            }
            }

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups