开发者

Help me to Get this code work

开发者 https://www.devze.com 2023-02-16 04:04 出处:网络
What is wrong with this code? i can get correct value? DrvUsg always getting zero. please help me to get work this code.

What is wrong with this code? i can get correct value? DrvUsg always getting zero. please help me to get work this code.

    Compu开发者_如何转开发ter 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));


This is the problem:

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

This will work:

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

You were doing an integer division which will always round down, since TotalDrv is larger than UsedDrv the result was always zero.

0

精彩评论

暂无评论...
验证码 换一张
取 消