C# 获取 Windows 服务启动的日期/时间
有没有办法获取 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 C# 应用程序中,写入
if processname 未运行,这会引发异常,修改以实现您想要的任何替代行为。此外,如果该进程的多个实例正在运行,则在最早的实例启动时返回...
In a C# app, write
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...
这应该为您进行正确的查找。根据需要修改!
This should do the proper lookup for you. Modify as necessary!
考虑到此信息在服务管理器中不可见,因此可能无法通过
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.