ryeguy
ryeguy

Reputation: 66851

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#.

Upvotes: 5

Views: 10265

Answers (6)

Nickiler
Nickiler

Reputation: 299

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

Upvotes: 1

Brian Paul
Brian Paul

Reputation: 651

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

Upvotes: 2

aleemb
aleemb

Reputation: 32095

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)

Upvotes: 1

David McEwing
David McEwing

Reputation: 3340

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.

Upvotes: 0

user27414
user27414

Reputation:

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).

Upvotes: 2

Ravedave
Ravedave

Reputation: 1188

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.

Upvotes: 2

Related Questions