Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Size of file in FileInfo?

Tags:

c#

I want to get the size to show in the ListView. But my code return 0. I find.Length() get length size but only have Count() and it can't resolve my problem.

My code like:

string[] filePaths = (string[])e.Data.GetData(DataFormats.FileDrop, false);
List<FileInfo> fileInfos = new List<FileInfo>();  
foreach (string filePath in filePaths)
{
    FileInfo f = new FileInfo(filePath);
    fileInfos.Add(f);
    long s1 = fileInfos.Count;
    string chkExt = Path.GetExtension(f.ToString());
    s1 = s1/1024;
    decimal d = default(decimal);
    d = decimal.Round(s1, 2, MidpointRounding.AwayFromZero);
    // d is 0. Because s1 = 1 only count not length of file.
like image 724
terri Avatar asked Dec 24 '22 10:12

terri


1 Answers

You are using fileInfos, which is a List<T>. You want to check the length of your actual FileInfo f:

long s1 = f.Length;

While you are dividing by 1024, please note, that there is a difference in units when dealing with filezises, depending on your base: 2 or 10. KB is 2^x and kB is 10^x bytes.

like image 197
Marco Avatar answered Jan 11 '23 09:01

Marco