简体   繁体   English

如何判断计算机是否已重新启动?

[英]How can I tell if the computer has been restarted?

I once used a command line SMTP mailer that, as a limit of the trial version, allowed you to receive up to 10 emails with it per Windows session. 我曾经使用命令行SMTP邮件程序,作为试用版的限制,每个Windows会话允许您最多接收10封电子邮件。 If you rebooted your computer, you could receive 10 more. 如果您重新启动计算机,则可以再接收10台计算机。 I thought this kind of shareware crippling was pretty neat, and I'd like to replicate it in my app. 我认为这种共享软件非常简洁,我想在我的应用程序中复制它。

I'm just stuck on how to do it. 我只是坚持如何做到这一点。 I know how to limit the user's actions, but how can I tell if the computer has been restarted since the application has been last run? 我知道如何限制用户的操作,但是如何在应用程序上次运行后重新启动计算机?

The OS is Windows and the language is C#. 操作系统是Windows,语言是C#。

For Windows, if you want to check the computer booted 对于Windows,如果要检查计算机是否已启动

Type on the Command Prompt 在命令提示符上键入
C:\\>net statistics server

One of the statistics will be 其中一项统计数据将是
Statistics since 22-Jun-11 10:46:20 which was the last time the computer booted Statistics since 22-Jun-11 10:46:20这是计算机最后一次启动

You should be able to find events in the event log, such as the event log service start that would tell you if the computer has restarted. 您应该能够在事件日志中查找事件,例如事件日志服务启动,它将告诉您计算机是否已重新启动。

Here's how to read the event log in C#: http://msdn.microsoft.com/en-us/library/k6b9a7h8%28VS.71%29.aspx 以下是如何在C#中读取事件日志: http//msdn.microsoft.com/en-us/library/k6b9a7h8%28VS.71%29.aspx

// C#
foreach (System.Diagnostics.EventLogEntry entry in EventLog1.Entries) 
{
   Console.WriteLine(entry.Message);
}

Note: you should provide the language and OS you are using. 注意:您应该提供您正在使用的语言和操作系统。

If you're using .NET, you can use Environment.TickCount for this. 如果您使用的是.NET,则可以使用Environment.TickCount This gives you the number of ms since the system started. 这将为您提供自系统启动以来的ms数。 Note that it rolls over every ~24 days, so you'll have to compensate for that (if you care). 请注意,它每隔~24天滚动一次,因此您必须对此进行补偿(如果您在意)。

The answer above is for Pre-Windows Server 2008 systems, but for Windows 2008 there is a much easier way of finding the uptime for a Windows 2008 server. 以上答案适用于Pre-Windows Server 2008系统,但对于Windows 2008,可以更轻松地查找Windows 2008服务器的正常运行时间。

  • Task Manager > Performance > Up Time 任务管理器>性能>运行时间

任务管理器

For Windows platform you can use uptime . 对于Windows平台,您可以使用正常运行时间

C:\>uptime
\\SPOCK has been up for: 2 day(s), 0 hour(s), 45 minute(s), 34 second(s)

Couldn't you just keep a counter of the number of events your application has performed. 难道你不能只保留你的应用程序执行的事件数量的计数器。 And then stop when the counter reached threshold? 当计数器达到阈值时停止? If your application contains a service then it could be embedded as part of the service which would be restarted with the windows sessions. 如果您的应用程序包含服务,那么它可以作为服务的一部分嵌入,该服务将通过Windows会话重新启动。 I suspect that is how the SMTP server worked, at least that is the simplest way that I would implement something like that. 我怀疑这是SMTP服务器的工作方式,至少这是我实现类似的最简单的方法。 It would keep most novice/intermediate system admins restarting the box, and the smart ones probably deserve to have the software for free anyway. 这将使大多数新手/中级系统管理员重新开箱,并且聪明的人可能应该免费获得该软件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM