tags:

views:

834

answers:

5

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

+2  A: 

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.

Ravedave
A: 

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

Jon B
+3  A: 
fretje
Can that be messed with by playing with the system time?
karim79
Not sure, but I think not. I think that SystemUpTime internally uses something like a tickcount.
fretje
A: 

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.

David McEwing
+1  A: 

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)
aleemb
Is this a Windows Server app? I don't seem to have this on WinXP.
ryeguy
Doesn't work on XP possibly other systems
Anton