728x90

    static void SortFiles(string title, Comparison<FileInfo> sortorder)
        {
            FileInfo[] files = new DirectoryInfo(@"C:\").GetFiles();
            Array.Sort(files, sortorder);
            Console.WriteLine(title);
            foreach (FileInfo f in files)
            {
                Console.WriteLine("{0},({1} bytes)", f.Name, f.Length);
            }
        }

 

        private void button1_Click(object sender, EventArgs e)
        {
            SortFiles("sort name", delegate(FileInfo first, FileInfo second){ return first.Name.CompareTo(second.Name); });

            SortFiles("sort length", delegate(FileInfo first, FileInfo second) { return first.Length.CompareTo(second.Length); });
        }

 

결과

sort name
backup.bak,(1921536 bytes)
backup_Dump.bak,(348672 bytes)
backup_Dump2.bak,(1921536 bytes)
bootmgr,(383786 bytes)
BOOTSECT.BAK,(8192 bytes)
grldr,(206376 bytes)
hiberfil.sys,(3220627456 bytes)
my_xml_file.xml,(102 bytes)
pagefile.sys,(4294172672 bytes)
pavel.xml,(133 bytes)
sort length
my_xml_file.xml,(102 bytes)
pavel.xml,(133 bytes)
BOOTSECT.BAK,(8192 bytes)
grldr,(206376 bytes)
backup_Dump.bak,(348672 bytes)
bootmgr,(383786 bytes)
backup.bak,(1921536 bytes)
backup_Dump2.bak,(1921536 bytes)
hiberfil.sys,(3220627456 bytes)
pagefile.sys,(4294172672 bytes)

 

728x90
Posted by kjun.kr
,