Measuring fileinfo.Length objects into kbs
Asked Answered
A

5

6

I have the following code:

foreach (string p in dirs)
        {
            string path = p;
            string lastAccessTime = File.GetLastAccessTime(path).ToString();
            bool DirFile = File.Exists(path);
            FileInfo fInf = new FileInfo(path);

            DateTime lastWriteTime = File.GetLastWriteTime(p);
            dirFiles.Add(p + "|" + lastAccessTime.ToString() + "|" + DirFile.ToString() + "|" + lastWriteTime.ToString() + "|" + fInf.Length.ToString());


        }

I have a fInf.Length.ToString() and i'd like to measure the output in terms of kbs. Any ideas on how do accomplish this? For example, instead of getting 2048 as a File Size, i'd like to just get 2Kb.

Thanks in advance for help

Apfelstadt answered 29/6, 2009 at 13:23 Comment(0)
B
19

Here's how to get it broken down in gigabytes, megabytes or kilobytes:

string sLen = fInf.Length.ToString();
if (fInf.Length >= (1 << 30))
    sLen = string.Format("{0}Gb", fInf.Length >> 30);
else
if (fInf.Length >= (1 << 20))
    sLen = string.Format("{0}Mb", fInf.Length >> 20);
else
if (fInf.Length >= (1 << 10))
    sLen = string.Format("{0}Kb", fInf.Length >> 10);

sLen will have your answer. You could wrap it in a function and just pass in the Length, or even the FileInfo object.

If instead of 'real' kilobytes, you wanted it in terms of 1000's of bytes, you could replace 1 << 10 and >> 10 with 1000 and /1000 respectively, and similarly for the others using 1000000 and 1000000000.

Bourdon answered 29/6, 2009 at 13:31 Comment(1)
I know this is long after your answer, but why not simply try FileInfo.Length / 1024; to get KBs, and then continue to divide to get the next measure up?Uhl
A
12

If you want the length as a (long) integer:

long lengthInK = fInf.Length / 1024;
string forDisplay = lengthInK.ToString("N0") + " KB";    // eg, "48,393 KB"

If you want the length as a floating point:

float lengthInK = fInf.Length / 1024f;
string forDisplay = lengthInK.ToString("N2") + " KB";    // eg, "48,393.68 KB"
Astomatous answered 29/6, 2009 at 13:26 Comment(0)
C
3

Try line below:

string sizeInKb = string.Format("{0} kb", fileInfo.Length / 1024);
Chartier answered 29/6, 2009 at 13:27 Comment(0)
G
1

Refactoring @lavinio answer a bit:

public static string ToFileLengthRepresentation(this long fileLength)
{
    if (fileLength >= 1 << 30)
        return $"{fileLength >> 30}Gb";

    if (fileLength >= 1 << 20)
        return $"{fileLength >> 20}Mb";

    if (fileLength >= 1 << 10)
        return $"{fileLength >> 10}Kb";

    return $"{fileLength}B";
}

[TestFixture]
public class NumberExtensionsTests
{
    [Test]
    [TestCase(1024, "1Kb")]
    [TestCase(2048, "2Kb")]
    [TestCase(2100, "2Kb")]
    [TestCase(700, "700B")]
    [TestCase(1073741824, "1Gb")]
    public void ToFileLengthRepresentation(long amount, string expected)
    {
        amount.ToFileLengthRepresentation().ShouldBe(expected);
    }
}
Gearard answered 19/1, 2020 at 14:28 Comment(0)
L
0

Here is a beautiful solution using the latest C# syntax sugar.

static string ToReadableFileSize(this long size) => size switch
{
    >= 1 << 30 => $"{size >> 30}Gb",
    >= 1 << 20 => $"{size >> 20}Mb",
    >= 1 << 10 => $"{size >> 10}Kb",
    _ => $"{size}B"
};
Lactoscope answered 28/4, 2023 at 22:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.