# C# 获取系统最后活动时间(已多久未活动)

代码

C#
private LASTINPUTINFO lastInputInfo = new LASTINPUTINFO();
private void InitLastInputInfo()
{
    lastInputInfo.cbSize = (uint)Marshal.SizeOf(typeof(LASTINPUTINFO));
}
private uint GetLastInputInfo()
{
    bool res = GetLastInputInfo(ref lastInputInfo);
    if (res)
    {
        return (uint)Environment.TickCount - lastInputInfo.dwTime;
    }
    return 0;
}
[DllImport("user32.dll")]
private static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);

[StructLayout(LayoutKind.Sequential)]
struct LASTINPUTINFO
{
    public uint cbSize;
    public uint dwTime;
}