我需要用 C# 开发一个程序,找出 Windows 何时启动或关闭。
是否有一个日志文件可供我读取以了解 Windows 启动和关闭时间?或者您有什么想法如何做到这一点?
EDIT :
在 Reed Copsey 先生的帮助下,在下面找到了最佳答案question https://stackoverflow.com/questions/6185608/getting-server-restart-count-in-a-day-using-c.
根据本文 http://web.archive.org/web/20111104234407/http://www.c-sharpcorner.com/UploadFile/scottlysle/TimeSinceBootCS06052008055807AM/TimeSinceBootCS.aspx您可以使用 WMI 来获取上次启动日期/时间.
// define a select query
SelectQuery query =
new SelectQuery(@"SELECT LastBootUpTime FROM Win32_OperatingSystem
WHERE Primary='true'");
// create a new management object searcher and pass it
// the select query
ManagementObjectSearcher searcher =
new ManagementObjectSearcher(query);
// get the datetime value and set the local boot
// time variable to contain that value
foreach(ManagementObject mo in searcher.Get())
{
dtBootTime =
ManagementDateTimeConverter.ToDateTime(
mo.Properties["LastBootUpTime"].Value.ToString());
// display the start time and date
txtDate.Text = dtBootTime.ToLongDateString();
txtTime.Text = dtBootTime.ToLongTimeString();
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)