Tutorial :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. If you rebooted your computer, you could receive 10 more. 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#.


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#  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. This gives you the number of ms since the system started. Note that it rolls over every ~24 days, so you'll have to compensate for that (if you care).


For Windows, if you want to check the computer booted

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


For Windows platform you can use uptime.

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


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.

  • Task Manager > Performance > Up Time

Task Manager


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. I suspect that is how the SMTP server worked, at least that is the simplest way that I would implement something like that. It would keep most novice/intermediate system admins restarting the box, and the smart ones probably deserve to have the software for free anyway.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »