问题
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#.
回答1:
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.
回答2:
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).
回答3:
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
回答4:
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)
回答5:
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
回答6:
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.
来源:https://stackoverflow.com/questions/946939/how-can-i-tell-if-the-computer-has-been-restarted