帮助我获取此代码的工作

时间:2011-03-12 04:57:08

标签: c#

这段代码有什么问题?我能得到正确的价值吗? DrvUsg总是为零。请帮我把这段代码搞定。

    Computer cmp = new Computer();
    string SysDrv = System.Environment.SystemDirectory.Substring(0, 2);
    UInt64 TotalDrv = Convert.ToUInt64(cmp.FileSystem.GetDriveInfo(SysDrv).TotalSize / 1024 / 1024);
    UInt64 FreeDrv = Convert.ToUInt64(cmp.FileSystem.GetDriveInfo(SysDrv).AvailableFreeSpace / 1024 / 1024);
    UInt64 UsedDrv = (TotalDrv - FreeDrv);
    UInt64 DrvUsg = Convert.ToUInt64((UsedDrv / TotalDrv) * 100);
    TrkDrvUsg.Value = (int)DrvUsg;
    LblDrvUsg.Text = (String.Format("System drive usage: {0}%", DrvUsg));

1 个答案:

答案 0 :(得分:5)

这是问题所在:

UInt64 DrvUsg = Convert.ToUInt64((UsedDrv / TotalDrv) * 100);

这将有效:

UInt64 DrvUsg = Convert.ToUInt64(100 * UsedDrv / TotalDrv);

你正在进行整数除法,它总是向下舍入,因为TotalDrv大于UsedDrv,结果总是为零。

相关问题