首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何知道Windows何时启动或关闭?

如何知道Windows何时启动或关闭?
EN

Stack Overflow用户
提问于 2011-09-13 19:23:25
回答 9查看 17.1K关注 0票数 12

我需要在C#中开发一个程序,找出Windows什么时候启动或关闭。

是否有我可以读取的日志文件来了解Windows的启动和关闭时间?或者你有什么办法吗?

编辑:

在里德·科普西先生的帮助下,最好的答案是在这个问题下面。

EN

回答 9

Stack Overflow用户

回答已采纳

发布于 2011-09-13 19:26:20

可以使用System.Diagnostics.Eventing.Reader中的类访问系统事件日志。

票数 4
EN

Stack Overflow用户

发布于 2011-09-13 19:28:15

根据这篇文章,您可以使用WMI获取上一次引导日期/时间

代码语言:javascript
复制
// 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();
}
票数 15
EN

Stack Overflow用户

发布于 2014-01-04 10:56:27

System.Environment.TickCount有24.8天的限制。

这是因为TickCount是一个毫秒值,包含在一个签名的32位值中。

Windows API公开了这两个函数:

GetTickCount -返回来自Windows2000的32位值

GetTickCount64 -返回64位值-可从Vista/Windows 2008获得

您可以这样使用GetTickCount64:

代码语言:javascript
复制
using System.Runtime.InteropServices;  

[DllImport("Kernel32.dll")]  
static extern long GetTickCount64();  

DateTime osStartTime = DateTime.Now - new TimeSpan(10000 * GetTickCount64());
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7407286

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档