我想知道系统最后一次启动是什么时候。
Environment.TickCount 可以工作,但由于 int 的限制,它会在 48-49 天后中断。
这是我一直在使用的代码:
Environment.TickCount & Int32.MaxValue
有人知道 long type return 吗?
我用它来了解系统的空闲时间:
public static int GetIdleTime()
{
return (Environment.TickCount & Int32.MaxValue)- (int)GetLastInputTime();
}
/// <summary>
/// Get the last input time from the input devices.
/// Exception:
/// If it cannot get the last input information then it throws an exception with
/// the appropriate message.
/// </summary>
/// <returns>Last input time in milliseconds.</returns>
public static uint GetLastInputTime()
{
LastInputInfo lastInPut = new LastInputInfo();
lastInPut.BlockSize = (uint)System.Runtime.InteropServices.Marshal.SizeOf(lastInPut);
if (!GetLastInputInfo(ref lastInPut))
{
throw new Exception(GetLastError().ToString());
}
return lastInPut.Time;
}
最佳答案
以下代码检索自系统启动以来的毫秒数(调用 unmanged API)。我测量了该互操作操作的性能成本,它与 StopWatch() 完全相同(当然,它不会检索自系统直接启动以来的时间)。
using System.Runtime.InteropServices;
...
[DllImport("kernel32.dll") ]
public static extern UInt64 GetTickCount64();
...
var tickCount64 = GetTickCount64();
https://msdn.microsoft.com/de-de/library/windows/desktop/ms724411(v=vs.85).aspx
关于c# - Environment.TickCount 是不够的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4645171/