C# 获取 Windows 服务启动的日期/时间

发布于 2024-08-14 19:14:39 字数 329 浏览 4 评论 0原文

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

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

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

我可以使用此对象执行此操作吗?或者需要WMI?

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

Is there a way to get the date/time that a service last started in C#?

I'm using this code now to check the status of services:

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

Can I do it with this object? Or need WMI?

Reason: I'm writing a little BizTalk Monitor, and a common problem is that people often forget to restart the BizTalk Service (host instances) after doing a deploy. I want to show the time it last was started.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

雪花飘飘的天空 2024-08-21 19:14:39

在 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 ;
 }

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

In a C# app, write

 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 ;
 }

if processname is not running, this throws an exception, modify to implement whatever alternative behavior you want. Also, if multiple instances of this process are running, this returns when the earliest was started...

拥醉 2024-08-21 19:14:39

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

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;
            }
        }

This should do the proper lookup for you. Modify as necessary!

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;
            }
        }
棒棒糖 2024-08-21 19:14:39

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

话虽这么说,请尝试查看事件日志 。启动和停止服务时会自动生成事件。

Considering this information isn't visible in the Services Manager, it probably can't be found through the ServiceController class (and I didn't see anything in there either).

That being said, try looking at the Event Log. An event is generated automatically when a service is started and stopped.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文