我的App.Config文件没有保存我修改的任何内容 [英] My App.Config file isn't saving anything I modify
问题描述
这是我的代码:
public void VerifyIfFirstTimeRun()
{
if (System.Configuration.ConfigurationSettings.AppSettings["FirstTimeRunning"] == "true")
{
//Do bla bla bla
//Then do bla bla bla
System.Configuration.ConfigurationSettings.AppSettings["FirstTimeRunning"] = "false";
}
}
问题是,我正在用F5键进行测试,它会启动,并确保使用断点足够显示它确实在If条件中,但是当我停止"应用程序并再次按F5时,它将消失如果条件再次出现在内部.这是标准的操作程序吗?
如果是这样,我如何测试它是否正常工作?
这与App.config文件用于...的精神背道而驰,但是要回答您的问题,您需要执行System.Configuration. Configuration.Save().
App.config通常用于配置数据库连接,提供程序等.通常在安装时设置一次.建议将用户设置放入单独的配置文件user.config中.参见此以获取说明.>
System.Configuration.Configuration 类.
注意-现在,我了解了为什么您使用这些设置,我可以建议另一种方法吗?您可能应该只检查文件是否存在:
if (!File.Exists("thefilepath"))
{
AlertUserOfMissingFile();
ChooseNewFile();
}
以这种方式更安全,因为将设置设置为true并不一定意味着文件就在您认为的位置.
Here's my code:
public void VerifyIfFirstTimeRun()
{
if (System.Configuration.ConfigurationSettings.AppSettings["FirstTimeRunning"] == "true")
{
//Do bla bla bla
//Then do bla bla bla
System.Configuration.ConfigurationSettings.AppSettings["FirstTimeRunning"] = "false";
}
}
Problem is, I'm testing this with the F5 key, it boots up and sure enough using a breakpoint shows that it is indeed going inside the If Condition, but when I "Stop" the application and press F5 again it goes inside the If Condition AGAIN. Is this standard operational procedures?
If so, how can I test if its working?
This is going against the spirit of what the App.config file is used for ... but to answer your question, you need to do System.Configuration.Configuration.Save().
Edit:
App.config is typically used to configure database connections, providers, etc. And is usually set once at installation. It's suggested that user settings go into a separate config file user.config. See this for the explanation.
Edit:
System.Configuration.Configuration class.
Note - now that I read why you're using these settings, may I suggest another way? You should probably just check if the file is there:
if (!File.Exists("thefilepath"))
{
AlertUserOfMissingFile();
ChooseNewFile();
}
It's safer this way anyhow because having a setting set to true doesn't necessarily mean the file is where you think it is.
这篇关于我的App.Config文件没有保存我修改的任何内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!