Is it possible to get the size of a file in C# without using System.IO.FileInfo
at all?
I know that you can get other things like Name and
Not a direct answer...because I am not sure there is a faster way using the .NET framework.
Here's the code I am using:
List list = new List();
DirectoryInfo di = new DirectoryInfo("C:\\Program Files");
FileInfo[] fiArray = di.GetFiles("*", SearchOption.AllDirectories);
foreach (FileInfo f in fiArray)
list.Add(f.Length);
Running that, it took 2709ms to run on my "Program Files" directory, which was around 22720 files. That's no slouch by any means. Furthermore, when I put *.txt
as a filter for the first parameter of the GetFiles
method, it cut the time down drastically to 461ms.
A lot of this will depend on how fast your hard drive is, but I really don't think that FileInfo is killing performance.
NOTE: I thikn this only valid for .NET 4+