I am using GetFiles and GetDirectories to retrieve files from a folder browser dialog. Now I have a problem with sorting the files/ directories by the size. I tried the following method but doesn't sure what is the next step to loop both files/folders based on their size.
private void Button1_Click(object sender, EventArgs e)
{
//open folder dialog
FolderBrowserDialog FBD = new FolderBrowserDialog();
if (FBD.ShowDialog() == DialogResult.OK)
{
listBox1.Items.Clear();
//get file name and file info
FileInfo[] files = new DirectoryInfo(FBD.SelectedPath).GetFiles();
//get directory
DirectoryInfo[] dirs = new DirectoryInfo(FBD.SelectedPath).GetDirectories();
//Order by size.
var sort = from fileName in files
orderby new FileInfo(fileName).Length ascending
select fileName;
//now I have no idea how to apply this sort to the loop below
foreach (FileInfo file in files)
{
//print single file output eg. filename (300000 bytes)
listBox1.Items.Add(file.Name + "(" + file.Length + " bytes)");
}
foreach (DirectoryInfo dir in dirs)
{
//print sub folder output eg. foldername (sum of all childfiles sizes) -> (600000 bytes)
listBox1.Items.Add(dir.Name + "(" + dir.GetFiles().Sum(f => f.Length) + " bytes)");
}
}
}
The button will open a folder dialog, and a listbox to display the result.