ComputerAFK.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.InteropServices;
  4. using System.Text;
  5. namespace Y.Utils.WindowsAPI
  6. {
  7. public class ComputerAFK
  8. {
  9. #region 系统空闲时间
  10. #region 捕获时间结构体
  11. [StructLayout(LayoutKind.Sequential)]
  12. struct LASTINPUTINFO
  13. {
  14. // 设置结构体块容量
  15. [MarshalAs(UnmanagedType.U4)]
  16. public int cbSize;
  17. // 捕获的时间
  18. [MarshalAs(UnmanagedType.U4)]
  19. public uint dwTime;
  20. }
  21. #endregion
  22. [DllImport("user32.dll")]
  23. private static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);
  24. /// <summary>
  25. /// 获取计算机无操作时间
  26. /// </summary>
  27. /// <returns></returns>
  28. public static long GetLastInputTime()
  29. {
  30. LASTINPUTINFO vLastInputInfo = new LASTINPUTINFO();
  31. vLastInputInfo.cbSize = Marshal.SizeOf(vLastInputInfo);
  32. // 捕获时间
  33. if (!GetLastInputInfo(ref vLastInputInfo))
  34. return 0;
  35. else
  36. return Environment.TickCount - (long)vLastInputInfo.dwTime;
  37. }
  38. #endregion
  39. }
  40. }