我最近在Stack Overflow 和谷歌上搜索了几天关于 app.config
的信息。
我正在编写一个程序,需要使用用户输入的值生成SQL脚本。最初,我使用app.config
存储一些默认值,在程序启动时加载到程序中。这很好用,直到我试图将新值存回到app.config
文件中。这时,我发现app.config
是只读的,我应该使用user.config
。
我有几个问题找不到答案:
是否建议使用
settings.Setting
来声明我想使用app.config
的所有值?或者手动输入就足够了?我一直在阅读关于
user.config
如何覆盖app.config
设置的内容。但当我更新我的user.config
文件时,程序仍然从原始的app.config
文件中读取。
这是我的包装类代码:
public NameValueCollection ReadSettings(string sectionName)
{
NameValueCollection scripts = null;
try
{
//read in the current values from the section
scripts = (NameValueCollection)ConfigurationManager.GetSection(sectionName);
if (scripts == null) throw new appConfigException(String.Format("The section {0} does not exists in app.config", sectionName));
}catch (Exception e){
//print out the log file
StreamWriter writer = new StreamWriter(DateTime.Now.ToString("d-MMM-yyyy") + "log.txt");
writer.WriteLine(e.ToString());
writer.Close();
//kill the application process so the user cannot advance further
System.Diagnostics.Process.GetCurrentProcess().Kill();
}
return scripts;
}
ConfigurationManager
是否应该自动知道从user.config
开始读取?还是我需要更改代码中的此部分以反映这一点?