tags:

views:

331

answers:

4

Hello,

C# 2008/3.5 SP1

Hello,

I want to check to see if the application is running for the first time. I have developed an application and once this is installed on the clients computer. I want to make a check if it is first time running.

I have installed using the windows installer project.

 if (System.Deployment.Application.ApplicationDeployment.CurrentDeployment.IsFirstRun)
 {
      // Do something here
 }

The above code works for a clickonce development. But how can I do something similar with a windows installer.

I was thinking of added a registery when the application installs. Then check this registery item, when the program runs for the first time (true). Once it has run for the first time edit the registry to (false).

However, rather then use the registry, is there a better method that I can use?

Many thanks,

+1  A: 

Registry sounds fine. You probably want to make sure you do all first-time initialization properly before setting the value to false, and you could have an option for the user to reset this if necessary.

Marcus Lindblom
+1  A: 

Rather than messing around with the registry, you could just store a file in the user's account folder. I can't recall the exact location, but there's a call you can do to get the location of the user settings folder.

Kibbee
+2  A: 

Hi,

A good place to store application data outside of the registry is the application data folder. If you create this directory on first run then you would just need to test for it on subsequent loads. Also if your application requires it you then have a good storage location for data.

        string dataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
        string appName = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name.ToString();
        string appPath = dataPath + "\\" + appName;

        // test to see if the folder exists
        if (!System.IO.Directory.Exists(appPath))
        {
            // first run
            System.IO.DirectoryInfo di = System.IO.Directory.CreateDirectory(appPath);
        }
        else
        {
            // application has been run
        }
Fraser
+3  A: 

Just add a boolean value to your Applications Settings. The simplest way is to use the IDE and the settings designer. It has to be user scope though, application scope is not writeable.

Don't forget to toggle the value and save the settings when you detect first-time.

The code looks like this:

 if (Properties.Settings.Default.FirstUse)
 {
     Properties.Settings.Default.FirstUse = false;
     Properties.Settings.Default.Save();

     // do things first-time only
 }
Henk Holterman