我想实时获取 CPU 性能数据,包括温度。我使用以下代码来获取 CPU 温度:
try
{
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("root\\WMI",
"SELECT * FROM MSAcpi_ThermalZoneTemperature");
foreach (ManagementObject queryObj in searcher.Get())
{
double temp = Convert.ToDouble(queryObj["CurrentTemperature"].ToString());
double temp_critical = Convert.ToDouble(queryObj["CriticalTripPoint"].ToString());
double temp_cel = (temp/10 - 273.15);
double temp_critical_cel = temp_critical / 10 - 273.15;
lblCurrentTemp.Text = temp_cel.ToString();
lblCriticalTemp.Text = temp_critical_cel.ToString();
}
}
catch (ManagementException e)
{
MessageBox.Show("An error occurred while querying for WMI data: " + e.Message);
}
Run Code Online (Sandbox Code Playgroud)
但此代码显示的温度不是正确的温度。它通常显示 49.5-50.5 摄氏度。但我使用了“OpenHardwareMonitor”,它报告 CPU 温度超过 71 摄氏度,并且随着时间分数的变化而变化。我在代码中遗漏了什么吗?
我在 timer_click 事件中每隔 500 毫秒使用上面的代码来刷新温度读数,但从执行开始它总是显示相同的温度。这意味着如果您运行此应用程序并且它显示 …
我有一个应用程序将用户信息与图像保存到数据库.管理员可以通过不同的表单视图访问已保存的信息.单击列表框项目将显示从数据库中检索的图像的详细信息.
UserViewDetails.cs:
private void lbEmp_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
if (lbEmp.SelectedIndex != -1)
{
em.Emp_ID = Convert.ToInt32(lbEmp.SelectedValue);
em.SelectById();
if (!em.EmptyPhoto)
pbEmp.BackgroundImage = em.Picture;
else
pbEmp.BackgroundImage = null;
txtEmpName.Text = em.Emp_Name;
txtImagePath.Text = em.ImgPath;
cmbEmpType.SelectedText = em.EmployeeType;
cmbCountry.SelectedValue = em.CountryID;
cmbCity.SelectedValue = em.CityID;
}
}
catch (Exception) { }
}
Run Code Online (Sandbox Code Playgroud)
从父表单调用此表单Form1
:
Form1.cs中:
try
{
var vi = new Admin.frmViewEmployeeInfo();
vi.ShowDialog();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Run Code Online (Sandbox Code Playgroud)
在这里,捕获了"内存不足"异常.怎么了?相同的代码不会在我的另一个应用程序中抛出任何异常.
在我的应用程序中,我正在做的事情是用户可以通过我的应用程序控制他/她的本地Windows用户帐户,即可以从我的应用程序中创建用户,设置/删除密码,更改密码以及调用密码到期策略。现在,在这一点上,我需要弄清楚如果用户要在下次登录时更改密码,那么会发生什么。正如许多论坛和博客所说的那样,我做了相应的编码:
下次登录时调用密码过期
public bool InvokePasswordExpiredPolicy()
{
try
{
string path = GetDirectoryPath();
string attribute = "PasswordExpired";
DirectoryEntry de = new DirectoryEntry(path);
de.RefreshCache(new string[] { attribute });
if(de.Properties.Contains("PasswordExpired"))
de.Properties[attribute].Value = 1;
de.CommitChanges();
return true;
}
catch (Exception)
{
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
在下次登录时提示密码过期。重置标志
public bool ProvokePasswordExpiredPolicy()
{
try
{
string path = GetDirectoryPath();
string attribute = "PasswordExpired";
DirectoryEntry de = new DirectoryEntry(path);
de.RefreshCache(new string[] { attribute });
de.Properties[attribute].Value = -1;
de.CommitChanges();
return true;
}
catch (Exception)
{
return false;
}
} …
Run Code Online (Sandbox Code Playgroud)