获得最后的窗口关闭事件的使用.NET的日期时间 [英] Get the date-time of last windows shutdown event using .NET
问题描述
有没有办法找出当系统是上次关机?
Is there a way to find out when the system was last shutdown?
我知道有一种方法来找出最后的开机时间用在 LastBootUpTime 属性的Win32_OperatingSystem 命名空间中的 WMI
I know there's a way to find out last boot up time using the LastBootUpTime property in Win32_OperatingSystem namespace using WMI.
有什么相似找出上次关机时间?
Is there anything similar to find out last shutdown time?
感谢。
推荐答案
(这里的一切是100%的礼貌<一href="http://stackoverflow.com/questions/1631933/get-the-date-time-of-last-windows-shutdown-event-using-net/1632171#1632171">JDunkerley's前面回答)的
解决的办法是,而是可以用较少的可实现从字节
阵列将的DateTime
办法使用 BitConverter
。以下六类线code语句做同样的,并给出正确的的DateTime
从注册表:
The solution is above, but the approach of going from a byte
array to DateTime
can be achieved with fewer statements using the BitConverter
.The following six lines of code do the same and give the correct DateTime
from the registry:
public static DateTime GetLastSystemShutdown()
{
string sKey = @"System\CurrentControlSet\Control\Windows";
Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(sKey);
string sValueName = "ShutdownTime";
byte[] val = (byte[]) key.GetValue(sValueName);
long valueAsLong = BitConverter.ToInt64(val, 0);
return DateTime.FromFileTime(valueAsLong);
}
这篇关于获得最后的窗口关闭事件的使用.NET的日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!