博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Windows获取系统的开机时长
阅读量:2242 次
发布时间:2019-05-09

本文共 4599 字,大约阅读时间需要 15 分钟。

注:获取的是开机时长,而不是开机时间

在网上看了很多教程,基本上都是获取系统的开机时间,而不是开机时长。360虽然可以获取到开机时长,但是这个计算方法是不准确的,并且程序员获取不到这个数据。话不多说,直接上干货。

附:卡机时间图

在这里插入图片描述

方法一:采用windows自带的事件查看器

步骤:“事件查看器---->windows日志---->系统---->筛选---->6013”

注:6013是事件ID
在这里插入图片描述

在这里插入图片描述在这里插入图片描述

上述记录的有。但这是系统的,我们又如何能够拿到这个数据尼?

  1. 新建一个记事本,写入以下代码
  2. 将后缀改为 .bat,运行
  3. 注:第一次运行出现的是系统运行的时间(也就是开机到现在电脑一共运行了多久),第二次以及往后显示的都是开机时长
@echo offCLSCOLOR 0aMODE con: COLS=30 LINES=8wevtutil qe system /f:text /q:*[System[(EventID=6013)]] >%temp%\Eventlog.txtfor /f "tokens=*" %%i in (%temp%\Eventlog.txt) do set str=%%iecho.echo.echo.echo %str%echo.echo.pauseexit

但说实话,这个数据很不准确。(目前我也没有查到这个数据时系统加载时间还是什么,知道的大佬告知一下)

方法二:采用第三方工具 BootRacer

4. 下载工具
5. 运行,就会显示开机时长 (这个数据时比较准确的)

在这里插入图片描述

并且它会记录历史开机时长,点击下面的 History即可看到。

但是我们如何获取到这个数据尼?

  1. 这个程序会在后台自动生成日志文件,记录开机的相关信息。 文件位置:C:\Users\Public\Documents\bootracer.log
  2. 文件中Writing FullBoot字段就是开机时长

但是有两种情况,生成的日志文件会不一样:

  1. 如果电脑是重启的,就会有 Writing FullBoot: 字段,直接解析即可
  2. 如果电脑是关机,再开机的,计算方法就不一样,需要找到 Opened Mutex BootRacerOpti 以及Use Logon Time 字段,两者相减即可
    在这里插入图片描述
    附代码:
string SubTime(string endTime, string startTime){
int end_h = atoi(endTime.substr(0, endTime.find_first_of(':')).c_str()); int end_m = atoi(endTime.substr(endTime.find_first_of(':') + 1, endTime.find_last_of(':') - endTime.find_first_of(':') - 1).c_str()); int end_s = atoi(endTime.substr(endTime.find_last_of(':') + 1).c_str()); int start_h = atoi(startTime.substr(0, startTime.find_first_of(':')).c_str()); int start_m = atoi(startTime.substr(startTime.find_first_of(':') + 1, startTime.find_last_of(':') - startTime.find_first_of(':') - 1).c_str()); int start_s = atoi(startTime.substr(startTime.find_last_of(':') + 1).c_str()); int hour = end_h - start_h; int minute = end_m - start_m; int second = end_s - start_s; int bootTime = 0; if (hour == 0) {
if (minute == 0) {
bootTime = second; } else {
bootTime = minute * 60 + second; } } else {
if (minute == 0) {
bootTime = hour * 60 * 60 + second; } else {
bootTime = hour * 60 * 60 + minute * 60 + second; } } return to_string(bootTime);}void ParseFile(){
const char* filePath = ""C:\\Users\\Public\\Documents\\bootracer.log"; ifstream fIn(filePath, ios::binary); if (!fIn.is_open()) {
//日志文件,打开文件失败 cout<< "Bootracer日志文件打开失败 错误码为:" << GetLastError(); return; } int str1_line = 0; int str2_line = 0; int line = 0; string endTime; string startTime; string buf; string str1 = "Writing FullBoot"; string str2 = "Opened Mutex BootRacerOpti"; string str3 = "Use Logon Time"; while (!fIn.eof()) {
string buf; buf.resize(100); getline(fIn, buf); ++line; int pos = 0; int str_pos1 = 0; int str_pos2 = 0; int str_pos3 = 0; while (buf[pos] != '\r' && str1.size() != str_pos1 && buf[pos] != '\0') {
if (buf[pos] != str1[str_pos1]) {
pos = 0; str_pos1 = 0; break; } ++pos; ++str_pos1; } while (buf[pos] != '\r' && str2.size() != str_pos2 && buf[pos] != '\0') {
if (buf[pos] != str2[str_pos2]) {
pos = 0; str_pos2 = 0; break; } ++pos; ++str_pos2; } while (buf[pos] != '\r' && str3.size() != str_pos3 && buf[pos] != '\0') {
if (buf[pos] != str3[str_pos3]) {
pos = 0; str_pos3 = 0; break; } ++pos; ++str_pos3; } if (str1.size() == str_pos1) {
string tmp = buf.substr(str1.size() + 2); tmp = tmp.substr(0, tmp.size() - 1); _bootTime = tmp; str1_line = line; } if (str2.size() == str_pos2) {
int tmp_pos = buf.find_last_of(' '); string tmp = buf.substr(tmp_pos + 1); tmp = tmp.substr(0, tmp.size() - 1); endTime = tmp; str2_line = line; } if (str3.size() == str_pos3) {
int tmp_pos = buf.find_last_of(' '); string tmp = buf.substr(tmp_pos + 1); tmp = tmp.substr(0, tmp.size() - 1); startTime = tmp; } } if (endTime.size() != 0 && startTime.size() != 0 && (str1_line + 1) != str2_line) {
_bootTime = SubTime(endTime, startTime); } fIn.close();}

转载地址:http://icwdb.baihongyu.com/

你可能感兴趣的文章
Linux下查看根目录各文件内存占用情况
查看>>
A星算法详解(个人认为最详细,最通俗易懂的一个版本)
查看>>
利用栈实现DFS
查看>>
(PAT 1019) General Palindromic Number (进制转换)
查看>>
(PAT 1073) Scientific Notation (字符串模拟题)
查看>>
(PAT 1080) Graduate Admission (排序)
查看>>
Play on Words UVA - 10129 (欧拉路径)
查看>>
mininet+floodlight搭建sdn环境并创建简答topo
查看>>
【linux】nohup和&的作用
查看>>
Set、WeakSet、Map以及WeakMap结构基本知识点
查看>>
【NLP学习笔记】(一)Gensim基本使用方法
查看>>
【NLP学习笔记】(二)gensim使用之Topics and Transformations
查看>>
【深度学习】LSTM的架构及公式
查看>>
【python】re模块常用方法
查看>>
剑指offer 19.二叉树的镜像
查看>>
剑指offer 20.顺时针打印矩阵
查看>>
剑指offer 21.包含min函数的栈
查看>>
剑指offer 23.从上往下打印二叉树
查看>>
Leetcode C++《热题 Hot 100-18》538.把二叉搜索树转换为累加树
查看>>
Leetcode C++《热题 Hot 100-21》581.最短无序连续子数组
查看>>