如何检查是否在程序运行的第一次? [英] How can I check if a program is running for the first time?

查看:117
本文介绍了如何检查是否在程序运行的第一次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据是否在程序运行在第一次或不上我的节目设置其显示。为了确定是否在程序运行在第一次我目前使用的是

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"; }

这似乎然而,有效地开展工作pretty的,如果.exe文件移动,从一个新的位置,它认为这是一个第一次运行时启动,我担心我这样做在一个混乱的时尚或许存在一个更有效的方式来测试程序第一次运行。有没有更好的方式来做到这一点?

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?

推荐答案

看来你的问题其实是,如果你移动执行到另一个位置/文件夹的同一 PC,它失去了某种程度上的事实,它已经至少运行一次的信息。

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.

使用 UserSettings ,在 Properties.Settings.Default.FirstRun 应该解决您的问题。

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

这样的事情,一个的伪code 的:

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.

这篇关于如何检查是否在程序运行的第一次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆