Help me to Get this code work
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.
精彩评论