我对数学有些生疏,所以我希望有人能帮助我。使用下面的代码我想要做到以下几点:根据安装的内存量,我想显示可用内存的百分比,而不是剩下多少兆字节。如何四舍五入数字并以百分比显示?
private void timer1_Tick(object sender, EventArgs e)
{
string memory;
int mem;
memory = GetTotalMemoryInBytes().ToString();
mem = Convert.ToInt32(memory);
mem = mem/1048576;
progressBar2.Maximum = mem;
progressBar2.Value = mem - (int)(performanceCounter2.NextValue());
label2.Text = "Available Memory: " + (int)(performanceCounter2.NextValue()) + "Mb";
}
//using Microsoft visual dll reference in c#
static ulong GetTotalMemoryInBytes()
{
return new Microsoft.VisualBasic.Devices.ComputerInfo().TotalPhysicalMemory;
}
任何特定的原因你获取总内存,将其转换为字符串,然后将字符串转换回数字? –
嗨马克...我是C#的新手,因此我的技能不好:) –