我需要在C#中开发一个程序,找出Windows什么时候启动或关闭。
是否有我可以读取的日志文件来了解Windows的启动和关闭时间?或者你有什么办法吗?
编辑:
在里德·科普西先生的帮助下,最好的答案是在这个问题下面。
发布于 2011-09-13 19:26:20
可以使用System.Diagnostics.Eventing.Reader中的类访问系统事件日志。
发布于 2011-09-13 19:28:15
根据这篇文章,您可以使用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();
}发布于 2014-01-04 10:56:27
System.Environment.TickCount有24.8天的限制。
这是因为TickCount是一个毫秒值,包含在一个签名的32位值中。
Windows API公开了这两个函数:
GetTickCount -返回来自Windows2000的32位值
GetTickCount64 -返回64位值-可从Vista/Windows 2008获得
您可以这样使用GetTickCount64:
using System.Runtime.InteropServices;
[DllImport("Kernel32.dll")]
static extern long GetTickCount64();
DateTime osStartTime = DateTime.Now - new TimeSpan(10000 * GetTickCount64());https://stackoverflow.com/questions/7407286
复制相似问题