C# 取得WINDOWS 開機多久 [c# get windows start time][C# get date/time a windows service started]

C# 取得WINDOWS 開機多久 [c# get windows start time][C# get date/time a windows service started]

C# 取得WINDOWS 開機多久 [c# get windows start time][C# get date/time a windows service started]


資料來源: https://stackoverflow.com/questions/1928933/c-sharp-get-date-time-a-windows-service-started


Code:

 DateTime dt1 = DateTime.Now;
            DateTime dt2 = ConsumeTime.GetStartTime("explorer");
            String StrNowTime = dt1.ToString("yyyy/MM/dd HH:mm:ss");
            String StrWindwsStartTime = dt2.ToString("yyyy/MM/dd HH:mm:ss");
            TimeSpan ts = dt1 - dt2;
            String StrWindowsUptime=ts.TotalHours.ToString();
            String StrMsg = String.Format("電腦啟動時間 : {0}\n目前時間 : {1}\n經過幾小時 : {2}", StrWindwsStartTime, StrNowTime, StrWindowsUptime);
            MessageBox.Show(StrMsg);
			
    public class ConsumeTime
    {
        private static Stopwatch m_stopWatch = new Stopwatch();
        private static String m_StrTitle = "";
        private static String m_StrStartFileLine = "";
        private static String m_StrEndFileLine = "";
        public static void Start(String StrInfor)
        {
            StackFrame CallStack = new StackFrame(1, true);
            m_StrStartFileLine = String.Format("File : {0} , Line : {1}", CallStack.GetFileName(), CallStack.GetFileLineNumber());
            m_StrTitle = StrInfor;

            m_stopWatch.Start();
        }
        public static void Stop()
        {
            StackFrame CallStack = new StackFrame(1, true);

            m_stopWatch.Stop();

            // Get the elapsed time as a TimeSpan value.
            TimeSpan ts = m_stopWatch.Elapsed;
            // Format and display the TimeSpan value.
            string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",ts.Hours, ts.Minutes, ts.Seconds,ts.Milliseconds / 10);
            
            m_StrEndFileLine = String.Format("File : {0} , Line : {1}", CallStack.GetFileName(), CallStack.GetFileLineNumber());

            MessageBox.Show(m_StrStartFileLine + " ~ " + m_StrEndFileLine + " consume time: " + elapsedTime, m_StrTitle);
        }

        public static DateTime GetStartTime(int processId=0)
        {
            Process processes = Process.GetProcessById(processId);
            // -----------------------------
            DateTime retVal = DateTime.Now;
            retVal = processes.StartTime;

            return retVal;
        }
        public static DateTime GetStartTime(string processName)
        {
            Process[] processes =
                Process.GetProcessesByName(processName);
            if (processes.Length == 0)
                throw new ApplicationException(string.Format(
                   "Process {0} is not running.", processName));
            // -----------------------------
            DateTime retVal = DateTime.Now;
            foreach (Process p in processes)
                if (p.StartTime < retVal)
                    retVal = p.StartTime;

            return retVal;
        }
    }



PS. 


01.explorer.exe 是 WINDOWS的桌面程式,經過測試 抓取他的啟動時間作為開機最為接近

02.雖然有人建議用『net statistics workstation』或 『systeminfo』 但是我在公司NB都會有很大的誤差(2022/07/13測試會出現2022/07/08的日期)


發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *