C# 将 fileinfo.Length 对象测量为 kbs

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1058281/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 07:11:40  来源:igfitidea点击:

Measuring fileinfo.Length objects into kbs

c#system.io.fileinfo

提问by yeahumok

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.

我有一个 fInf.Length.ToString(),我想用 kbs 来衡量输出。关于如何做到这一点的任何想法?例如,我不想获得 2048 作为文件大小,而是只想获得 2Kb。

Thanks in advance for help

预先感谢您的帮助

采纳答案by lavinio

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);

sLenwill have your answer. You could wrap it in a function and just pass in the Length, or even the FileInfoobject.

sLen会有你的答案。您可以将它包装在一个函数中,然后传入Length,甚至是FileInfo对象。

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

如果不是“真正的”千字节,您需要 1000 字节的字节数,您可以分别用和替换1 << 10>> 10,对于其他使用 1000000 和 1000000000 的类似。1000/1000

回答by LukeH

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"

回答by Juanma

Try line below:

试试下面的行:

string sizeInKb = string.Format("{0} kb", fileInfo.Length / 1024);

回答by xleon

Refactoring @lavinio answera bit:

重构@lavinio 的回答

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);
    }
}