【问题标题】:C# get date/time a windows service startedC#获取Windows服务启动的日期/时间
【发布时间】:2009-12-18 15:40:05
【问题描述】:

有没有办法在 C# 中获取服务上次启动的日期/时间?

我现在正在使用这段代码来检查服务的状态:

ServiceController sc = new ServiceController(serviceName);
// check sc.status for "Running" etc... with a Switch statement... 

我可以用这个对象做吗?还是需要 WMI?

原因:我正在编写一个小 BizTalk Monitor,一个常见的问题是人们在进行部署后经常忘记重新启动 BizTalk 服务(主机实例)。我想显示上次启动的时间。

【问题讨论】:

    标签: c# wmi service process event-log


    【解决方案1】:

    在 C# 应用程序中,编写

     using System.Diagnostics;
     private 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 ;
     }
    

    如果 processname 没有运行,这将引发异常,修改以实现您想要的任何替代行为。此外,如果此进程的多个实例正在运行,则返回最早的启动时...

    【讨论】:

    • 它仍然没有回答问题,这就是我的意思。他要求找出“服务上次启动的时间”。使用这种方法,只能找到服务当前运行的时间。
    • 哦,对不起,当您“停止”服务时,您认为会发生什么?进程终止。这将返回进程上次运行时的 DateTime,对于服务,它是上次启动的时间。
    • @Charles:我想他的意思是,如果服务没有启动,那么就会抛出异常。我刚刚在 Windows Telnet 服务上尝试过,抛出了异常。如果服务被停止或禁用,这不起作用
    • @Roboto:但是 OP 这样做的原因是在部署后重新启动 BizTalk 服务,所以如果服务没有启动,那也可能没问题 - 只需启动服务。
    • @Uros:明白了,但他想要 /last/ 开始的时间。重新开始,不会给你这个..我不知道他为什么需要它,但这只有在它开始时才有效
    【解决方案2】:

    这应该会为您进行正确的查找。根据需要修改!

    public List<Hashtable> GetEventEntryByEvent(
                ref string logName, ref string machineName, 
                ref string source)
            {
                try
                {
                    //Create our list
                    List<Hashtable> events = new List<Hashtable>();
    
                    //Connect to the EventLog of the specified machine
                    EventLog log = new EventLog(logName, machineName);
    
                    //Now we want to loop through each entry
                    foreach (EventLogEntry entry in log.Entries)
                    {
                        //If we run across one with the right entry source
                        //  we create a new Hashtable
                        //  then we add the Message,Source, and TimeWritten values
                        //  from that entry
                        if (entry.Source == source)
                        {
                            Hashtable entryInfo = new Hashtable();
    
                            entryInfo.Add("Message", entry.Message);
                            entryInfo.Add("InstanceId", entry.InstanceId);
                            entryInfo.Add("Source", entry.Source);
                            entryInfo.Add("TimeWritten", entry.TimeWritten);
                            // You can also replace TimeWritten with TimeGenerated
                            //Add this new Hashtable to our list
                            events.Add(entryInfo);
    
                            entryInfo = null;
                        }
                    }
                    //Return the results
                    return events;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                    return null;
                }
            }
    

    【讨论】:

      【解决方案3】:

      考虑到此信息在服务管理器中不可见,它可能无法通过 ServiceController 类找到(我也没有在其中看到任何内容)。

      话虽如此,请尝试查看Event Log。服务启动和停止时会自动生成一个事件。

      【讨论】:

      • @Jon:这个信息在答案中会很好。告诉他查看事件日志并不意味着使用 System.Diagnostics.EventLog... 更好的答案甚至是包含代码示例
      • @Roboto:我认为既然他熟悉 ServiceController 类,他也会知道 EventLog 类。不过,我会编辑我的答案。
      • @Charles Bretana:如果它不是运行多个服务的 svchost.exe 进程之一。但我对 BizTalk 不熟悉。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多