asp.net 获取计算机启动时间

获取计算机从启动到现在总共运行的时间,在asp.net中有现成的类使用(Environment),也可以使用WinAPI
 

    #region 通过Framework类库获取系统启动时间
    private string GetStartTimesByFramework()
    {
        int totalSeconds = Environment.TickCount / 1000;
        int hour = totalSeconds / 3600;
        int minute = totalSeconds % 3600 / 60;
        int second = totalSeconds % 60;
        string startTimes=Convert.ToString(hour + "小时," + minute + "分," + second + "秒.");
        return startTimes;
    }
    #endregion

    #region 通过WinAPI来获取系统启动时间
    [DllImport("kernel32.dll", SetLastError = true)]
    static extern bool GetSystemTimes(out FILETIME lpIdleTime, out FILETIME lpKernelTime, out FILETIME lpUserTime);
    struct FILETIME
    {
        public uint DateTimeLow;
        public uint DateTimeHigh;

    }
    private string GetStartTimesByAPI()
    {
        FILETIME lpIdleTime, lpKernelTime, lpUserTime;
        GetSystemTimes(out   lpIdleTime, out   lpKernelTime, out   lpUserTime);
        ulong lngKernelTime = ((ulong)lpKernelTime.DateTimeHigh << 32) + lpKernelTime.DateTimeLow;
        ulong lngUserTime = ((ulong)lpUserTime.DateTimeHigh << 32) + lpUserTime.DateTimeLow;
        double dRunTime = lngKernelTime + lngUserTime;
        int totalSeconds = Convert.ToInt32(dRunTime / TimeSpan.TicksPerMillisecond / 1000);
        int hour = totalSeconds / 3600;
        int minute = totalSeconds % 3600 / 60;
        int second = totalSeconds % 60;
        string startTimes= Convert.ToString(hour + "小时," + minute + "分," + second + "秒.");
        return startTimes;
    }
    #endregion

转载于:https://www.cnblogs.com/tengs2000/articles/1058724.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值