gpt4 book ai didi

c# - 如何检查程序是否是第一次运行?

转载 作者:可可西里 更新时间:2023-11-01 09:01:20 24 4
gpt4 key购买 nike

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

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

当程序运行时,它会调用一个方法来检查该 bool 值的状态并采取相应的行动:

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

它似乎工作得非常有效,但是如果 .exe 文件被移动并从一个新位置启动,它认为这是第一次运行,我担心我正在以一种困惑的方式做这件事,也许存在一种更有效的方式来测试程序的首次运行。有更好的方法吗?

最佳答案

似乎您的问题实际上是,如果您将 executable 移动到 same pc 上的另一个位置/文件夹,它会以某种方式丢失有关它已经存在的信息至少运行一次。

Properties.Settings.Default.FirstRun 上使用 UserSettings 应该可以解决您的问题。

类似这样的伪代码:

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

this示例如何以更详细的方式实现这一目标。

关于c# - 如何检查程序是否是第一次运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8773748/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com