这段代码有什么问题?我能得到正确的价值吗?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));
Run Code Online (Sandbox Code Playgroud)
这就是问题:
UInt64 DrvUsg = Convert.ToUInt64((UsedDrv / TotalDrv) * 100);
Run Code Online (Sandbox Code Playgroud)
这将有效:
UInt64 DrvUsg = Convert.ToUInt64(100 * UsedDrv / TotalDrv);
Run Code Online (Sandbox Code Playgroud)
你正在做一个整数除法,它总是向下舍入,因为TotalDrv大于UsedDrv结果总是为零.