using System; using System.Collections.Generic; using System.Runtime.InteropServices; using System.Text; namespace Y.Utils.WindowsAPI { public class ComputerAFK { #region 系统空闲时间 #region 捕获时间结构体 [StructLayout(LayoutKind.Sequential)] struct LASTINPUTINFO { // 设置结构体块容量 [MarshalAs(UnmanagedType.U4)] public int cbSize; // 捕获的时间 [MarshalAs(UnmanagedType.U4)] public uint dwTime; } #endregion [DllImport("user32.dll")] private static extern bool GetLastInputInfo(ref LASTINPUTINFO plii); /// /// 获取计算机无操作时间 /// /// public static long GetLastInputTime() { LASTINPUTINFO vLastInputInfo = new LASTINPUTINFO(); vLastInputInfo.cbSize = Marshal.SizeOf(vLastInputInfo); // 捕获时间 if (!GetLastInputInfo(ref vLastInputInfo)) return 0; else return Environment.TickCount - (long)vLastInputInfo.dwTime; } #endregion } }