0

I made exe of my window application.I want that, when my exe installed on system and run first time to machine.I want to check if it is running first time, then system will reboot automatically, and after running first time, system should not be reboot every time.

Can i do this ? Please help me.

Thanks in advance.

jeet
  • 11
  • 1
  • 2
  • Use the registry entry to store flag that indicates the app first time launch.. any how the registry will clear on uninstall. – Shashi Dec 13 '11 at 04:52

5 Answers5

4

One option is to use a value in the user's program settings. Initially true, you can set it to false, like so:

if (Properties.Settings.Default.IsFirstTime)
{
    Properties.Settings.Default.IsFirstTime = false;
}

I also suggest adding a "UpgradeRequired" boolean to the settings which is by default true. When the user installs a newer version of the program, you need to call

Properties.Settings.Default.Upgrade();

and set

Properties.Settings.Default.UpgradeRequired = false;

and then save the settings:

Properties.Settings.Default.Save();

The reason for this is that if you install a new version, the IsFirstTime will be reset to its default value (unless you take up the current value, using Upgrade()).

This way, you don't need to worry about the registry, and you don't need to worry about file permissions.

Here it is, put all together:

// this must happen as soon as your program starts, before
// you do anything else with the settings
if (Properties.Settings.Default.UpgradeRequired)
{
    // upgrade FIRST, before doing anything else with the settings

    Properties.Settings.Default.Upgrade();
    Properties.Settings.Default.UpgradeRequired = false;
    Properties.Settings.Default.Save();
}


if (Properties.Settings.Default.IsFirstTime)
{
    // this is the first time running the program

    Properties.Settings.Default.IsFirstTime = false;
    Properties.Settings.Default.Save(); 
}

This has worked just fine for our customers.

Charlie Salts
  • 13,109
  • 7
  • 49
  • 78
3

I can't believe I'm saying this, but you want to check if a file exists.

If the file does not exist, prompt the user for permission and then create it and restart the system. You could also choose to delete a temporary file that deploys with your app, but I prefer to create a file because you can also use the file as a default settings file or initial database.

Notice that at no point in here do I ever suggest reading from the file, and so this is still compatible with the position in the linked question.

I also need to ask why you're restarting the system. Nothing in windows or in the .Net framework requires this. This is likely just going to be annoying for your users.

Community
  • 1
  • 1
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
0

Hello, this seems very easy to do.

All you have to do, is press Alt + Enter, or go to your properties.

Then, you go to the "Settings" tab.

After that, you do in the name box: "firstTime".

Then, you can type in the other box "true".

After that, you need to do this code:

if (Properties.Settings.Default.firstTime == "true") //Checks if the "firstTime" text = true
{ //If so, it does this code
  // Whatever you want here

  Properties.Settings.Default.firstTime = "false"; //Then it disables it, and saves it.
  Properties.Settings.Default.Save();
}

It's as easy as that!

Yaobin Then
  • 2,662
  • 1
  • 34
  • 54
James
  • 11
  • 1
0

I think you could use a registry entry for this. Check a key in the registry when application is executed, and if it's not set it to some value. If the registry key is not there means application is executed for the first time.

Low Flying Pelican
  • 5,974
  • 1
  • 32
  • 43
  • 1
    The problem here is writing that registry key requires administrator permissions... but then since we're talking about restarting the system that might not be a bad thing. – Joel Coehoorn Dec 13 '11 at 04:48
0

You can add a status_flag field into your database table and fill it up on first run of application. You need to check that field if it already contains a value, if it does, then it isn't the first run.

Dewasish Mitruka
  • 2,716
  • 1
  • 16
  • 20