How to determine if an application is first run after a PC reboots

  • Thread starter Thread starter aye2003
  • Start date Start date
A

aye2003

I am writing an application that needs to distinguish the first time it is
run after the PC is rebooted from the subsequent runs. I can think of a
possible solution: use Environment.TickCount to calculate the DateTime the
system was started, look for a dummy file's modified time to find out if the
dummy file was modified before or after the system was started, then touch
the dummy file to update its modified time stamp.

Is there a direct way to find out the DateTime when the system was started?
Any better way of determining if an application is run the first time after
the system starts? I also looked at the Run and RunOnce register keys, but
can't figure out how to use them for this purpose.

Angie
 
Hi Angie,

You could use the registry. If your program can't find a certain key it
is the first time it runs, otherwise it is any number of times. Write the
key in the registry if it doesn't find it. You could also use it as a
tick counter if you like. Or you could just check if the dummy file
exists. If not, it is the first time, and you create it. Or from a setup
program, create a dummy file and delete it on the first run.

....
RunOnce is usually used when the program needs to reboot during an
installation procedure to make sure the installation continues after the
reboot. Run will start up your program on every reboot.

Or... do you mean you want to find out if your application runs before
anything else after a reboot? As a first of a range of applications that
will start? In that case I have no idea.

Happy coding!
Morten
 
Sorry I did not say it clearly. My intention is to let the application
perform a special function each time after the system reboots, but the
function only needs to be performed once after each reboot. If the user
closes the application and restarts it, the function should not be
performed.

My application will be automatically started after the PC reboots (it is in
the Run registry list). I can write a dummy bat file dummy.bat that contains
something like "echo dummytext > dummytext.txt" and put dummy.bat in the Run
registry. But I am not sure how to guarantee that dummy.bat runs before my
application runs. What is the order the applications in the Run registry
run? Alphabetically? Based on the order they appear in the Run registry?

Angie
 
aye2003 said:
Sorry I did not say it clearly. My intention is to let the application
perform a special function each time after the system reboots, but the
function only needs to be performed once after each reboot. If the user
closes the application and restarts it, the function should not be
performed.

My application will be automatically started after the PC reboots (it is in
the Run registry list). I can write a dummy bat file dummy.bat that contains
something like "echo dummytext > dummytext.txt" and put dummy.bat in the Run
registry. But I am not sure how to guarantee that dummy.bat runs before my
application runs. What is the order the applications in the Run registry
run? Alphabetically? Based on the order they appear in the Run registry?

Why not use a command line flag? Set up the command in the Run registry to use
the flag to perform your initialization.

Run registry command (obviously substituting your application path:

C:\Program Files\Your App Folder\MyApp.exe /init

Then, in your application, process the command line, and if /init is specified,
you know to perform your initialization, otherwise without the flag (the user
restarted the app), don't initialize.
 
Back
Top