简体   繁体   English

如何比较两个自由空间?

[英]How I can compare two freespaces?

I have a text file where I put Space Required, that value (in kB) I extracted with : 我有一个文本文件,在其中放置了“需要空间”,该值是我用以下命令提取的:

string SpaceRequired = lines.Where(txt => txt.Contains("SpaceRequired"))
                            .Select(txt => txt.Split('=')[1].Replace("\"", ""))
                            .FirstOrDefault();

and the freeSpace that I need to compare it's here : 和我需要比较的freeSpace在这里:

var drive = new DriveInfo("c");
long freeSpaceInC = drive.TotalFreeSpace;

private static string toReadableSize(long size)
{
    if (size < 1024 * 1024)
        return Math.Round(((float)size / 1024), 2) + "KB";

    if (size < 1024 * 1024 * 1024)
        return Math.Round(((float)size / (1024 * 1024)), 2) + "MB";

    return Math.Round(((float)size / (1024 * 1024 * 1024)), 2) + "GB";
}

This comparison I tried to do here : 我在这里尝试进行此比较:

private void Next_Click(object sender, EventArgs e)
{
    if (textBox1.Text.StartsWith("C:"))
    {
        /*if (freeSpaceInC >= SpaceRequired)
        {
            this.Visible = false;
            Form2 fm = new Form2();
            fm.ShowDialog();
        }
        else
            MessageBox.Show("You don't have free space");*/
    }

but I don't know to do this. 但我不知道这样做 I need to compare the value in kB, because in text file I have in kB . 我需要比较kB中的值,因为在文本文件中我具有kB。 How I can do this ? 我该怎么做?

You have to parse the string, then you have the kb as long . 你必须解析字符串,那么你就有KB为long Since TotalFreeSpace returns the total amount of free space available on a drive in bytes you have to multiply it with 1000: 由于TotalFreeSpace以字节为单位返回驱动器上可用的可用空间总量,因此必须将其乘以1000:

long reqKB = long.Parse(SpaceRequired);
long reqBytes = 1000 * reqKB;
if (freeSpaceInC >= reqBytes)
{
    // ..
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM