C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
GetFiles: The program uses the Directory.GetFiles method found in the System.IO namespace.
FileTip: This is the easiest and normally the best way to get a string array containing each file name.
Array: The identifier "fns" is used to reference the string array. An English word would be better style.
ArrayC# program that sorts files by size
using System;
using System.IO;
using System.Linq;
class Program
{
static void Main()
{
// Directory of files.
const string dir = "C:\\site";
// File names.
string[] fns = Directory.GetFiles(dir);
// Order by size.
var sort = from fn in fns
orderby new FileInfo(fn).Length descending
select fn;
// List files.
foreach (string n in sort)
{
Console.WriteLine(n);
}
}
}
Output
(List of all files in the directory)
Tip: The descending contextual keyword is used in this example and you can find more information on this useful keyword.
Descending