我知道移动推车上大约有 250 台笔记本电脑没有得到充分利用。我正在编写一个程序,该程序编写一个日志文件来捕获开机和关机事件。但是,当我尝试捕获立即关闭事件(用户按住电源按钮)时,我没有将数据写入文本文件。
任何输入都会有所帮助。提前致谢。
代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Microsoft.Win32;
namespace Log
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string CrLf = System.Environment.NewLine;
private static int WM_QUERYENDSESSION = 0x11;
private static int WM_ENDSESSION_CRITICAL = 0x40000000;
private static bool systemShutdown = false;
private void Form1_Load(object sender, EventArgs e)
{
Write_Data("[" + DateTime.Now.ToString() + "] " + "Power on");
}
protected override void WndProc(ref System.Windows.Forms.Message m)
{
if (m.Msg == WM_QUERYENDSESSION)
{
Write_Data("[" + DateTime.Now.ToString() + "] " + "Power off");
systemShutdown = true;
}
// If this is WM_QUERYENDSESSION, the closing event should be
// raised in the base WndProc.
if (m.Msg == WM_ENDSESSION_CRITICAL)
{
Write_Data("[" + DateTime.Now.ToString() + "] " + "Critical power down");
systemShutdown = true;
}
base.WndProc(ref m);
}
private void Write_Data(string str_Data)
{
using (StreamWriter output = new StreamWriter("../Log.txt", true))
{
output.WriteLine(str_Data);
}
}
}
}