get system boot time in minutes in csharp

Here's a code snippet to get the system boot time in minutes using C#:

main.cs
using System;
using System.Runtime.InteropServices;

public class SystemUptime
{
    [DllImport("kernel32")]
    private static extern bool GetSystemTimes(out long idleTime, out long kernelTime, out long userTime);

    public static void Main(string[] args)
    {
        long idleTime, kernelTime, userTime;
        if (GetSystemTimes(out idleTime, out kernelTime, out userTime))
        {
            long tickCount = Environment.TickCount;
            long uptime = (tickCount * 10000) + idleTime + kernelTime + userTime;
            long uptimeMinutes = uptime / (60 * 10000000);
            Console.WriteLine("System uptime: " + uptimeMinutes + " minutes");
        }
    }
}
682 chars
21 lines

This code uses the GetSystemTimes() function from kernel32.dll to get the idle, kernel, and user time for the system. It then calculates the uptime by adding those values to the tick count (which represents the number of milliseconds since the system started). Finally, it converts the uptime to minutes and outputs the result to the console.

related categories

gistlibby LogSnag