Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
469 views
in Technique[技术] by (71.8m points)

c# - How can I check if a program is running for the first time?

My program sets its display based on if the program is running for the first time or not. In order to determine if the program is running for the first time I am currently using a

//this boolean exists within my programs settings
Setting boolean FirstRun = True;

When the program runs it calls a method that checks the state of that bool value and acts accordingly:

if(Properties.Settings.FirstRun == true)
{ lblGreetings.Text = "Welcome New User";
  //Change the value since the program has run once now
  Properties.Settings.FirstRun = false;
  Properties.Settings.Save(); }
else
{ lblGreetings.Text = "Welcome Back User"; }

It seems to work pretty effectively, however if the .exe file is moved and launched from a new location it considers it a first run, and I'm concerned that I'm doing this in a messy fashion and perhaps there exists a more efficient manner to test for the programs first run. Is there a better way to do this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Seems that your problem is actually that if you move executable to another location/folder on the same pc, it loses somehow the information about the fact that it was already run at least once.

Using UserSettings, on Properties.Settings.Default.FirstRun should resolve your problem.

Something like this, a pseudocode:

if(Properties.Settings.Default.FirstRun == true)
{ lblGreetings.Text = "Welcome New User";
  //Change the value since the program has run once now
  Properties.Settings.Default.FirstRun = false;
  Properties.Settings.Default.Save(); }
else
{ lblGreetings.Text = "Welcome Back User"; }

Look on this sample how to achieve that in more detailed way.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...