user12136320
user12136320

Reputation:

Show a Messagebox if program is ran for the first time in C#

When a user runs the program for the first time I want a message box to show up.

I was thinking of something like this:

private void Form1_Load(object sender, EventArgs e)
{
   if(firstTime)
   {
       MessageBox.Show("Welcome");
   }

How could I get my program to display a message box when a user launches the program for the first time in c#?

Upvotes: 1

Views: 103

Answers (2)

user12031933
user12031933

Reputation:

You can add a parameter to the application settings.

Go to the solution explorer in the section Properties and double click on Settings.settings.

Add a parameter named for example IsFirstLaunch and set type to bool with value True.

Then you can write:

if ( Properties.Settings.Default.IsFirstLaunch )
{
  Properties.Settings.Default.IsFirstLaunch = false;
  Properties.Settings.Default.Save();
  MessageBox.Show("Welcome");
}

The settings are stored in:

c:\Users\{UserName}\AppData\Local\{Assembly CompanyName}\{Assembly Name}.Url__________

So be careful to set Assembly CompanyName in the AssemblyInfo.cs in the same section.

Assembly Name is from the application project properties (double click on this Properties section).

You can delete this file to test again.

Upvotes: 1

madoxdev
madoxdev

Reputation: 3880

You would need to store that information somewhere

  • File
  • System registry
  • Database
  • Settings in application

Then read the value, and setup the firstTime flag prior to check.

Upvotes: 2

Related Questions