简体   繁体   中英

How to get sizes of files in directory?

I want to to list all files sizes in some directory. In java I would use the following code:

for (File file: Paths.get(directory).toFile().listFiles()) {
    System.out.println(file.length());
}

How can I achieve the same in C#?

Try this demo code:

class Program
{
    static void Main(string[] args)
    {
        var path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

        var files = System.IO.Directory.EnumerateFiles(path);

        Console.WriteLine($"{"Filename",-18} {"Size (bytes)"}");
        foreach (var file in files)
        {
            var info = new System.IO.FileInfo(file);
            Console.WriteLine($"{info.Name,-18} {info.Length}");
        }
    }
}

scr1

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