使用 .NET 获取上次 Windows 关闭事件的日期时间 [英] Get the date-time of last windows shutdown event using .NET

查看:18
本文介绍了使用 .NET 获取上次 Windows 关闭事件的日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法知道系统上次关机的时间?

Is there a way to find out when the system was last shutdown?

我知道有一种方法可以使用 WMI 使用 Win32_OperatingSystem 命名空间中的 LastBootUpTime 属性找出上次启动时间.

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% 由 JDunkerley 的早期回答)

解决方案如上,但从 byte 数组到 DateTime 的方法可以使用 BitConverter 用更少的语句来实现.以下六行代码执行相同操作,并从注册表中给出正确的 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 = @"SystemCurrentControlSetControlWindows";
    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 获取上次 Windows 关闭事件的日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆