ConfigurationManager 不保存设置
这是我正在使用的代码:
private void SaveConfiguration()
{
if (txtUsername.Text != "" && txtPassword.Text != "")
{
ConfigurationManager.AppSettings["Username"] = txtUsername.Text;
ConfigurationManager.AppSettings["Password"] = txtPassword.Text;
MessageBox.Show("Su configuracion guardo exitosamente.", "Exito!");
this.Close();
}
else
{
MessageBox.Show("Por favor lleno los campos.", "Error.");
}
}
现在,设置已保留,但当我关闭应用程序并按 F5 再次运行它时,这些值将恢复为在 app.config 文件中输入的内容。有什么建议吗?
Here is the code I'm using:
private void SaveConfiguration()
{
if (txtUsername.Text != "" && txtPassword.Text != "")
{
ConfigurationManager.AppSettings["Username"] = txtUsername.Text;
ConfigurationManager.AppSettings["Password"] = txtPassword.Text;
MessageBox.Show("Su configuracion guardo exitosamente.", "Exito!");
this.Close();
}
else
{
MessageBox.Show("Por favor lleno los campos.", "Error.");
}
}
Now, the settings are persisted but when I close the application and press F5 to run it again, the values are reverted to what is typed into the app.config file. Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为您应该调用 Save 方法
编辑
为了能够保存,您必须使用 OpenExeConfiguration 方法返回的配置对象
更多参考此处ConfigurationManager 类
I think you should call the Save method
EDIT
To be able to save you have to use a configuration object returned by the OpenExeConfiguration Method
More references here ConfigurationManager Class
当您使用 F5 运行应用程序时,
bin
或bin\Debug
子目录中,app .config
将作为yourexecutable.exe.config
复制到该目录中,并且因此,您的应用程序使用
bin
或bin\Debug
目录中的yourexecutable.exe.config
,并且它位于那里ConfigurationManager
保存更改,而不是在源代码目录中。部署应用程序后,这不会成为问题,因为这样,更改将转到部署目录中的yourexecutable.exe.config
,这正是您想要的。When you run your application with F5,
bin
orbin\Debug
subdirectory of your source code directory,app.config
is copied asyourexecutable.exe.config
into that directory, andThus, your application uses the
yourexecutable.exe.config
in thebin
orbin\Debug
directory, and it is there thatConfigurationManager
saves the changes, not in your source code directory. This won't be an issue after deploying your application, because then, changes will go toyourexecutable.exe.config
in the deployment directory, which is what you want.根据 Appetere 对第二个答案的评论:
另请注意,如果您正在调试(并且没有禁用 vshost 进程),那么当您的应用程序停止时,yourexecutable.vshost.exe.config 将被替换为 yourexecutable.exe.config再次。
因此,您可能看不到之后所做的任何更改! (如果您在调试时停在断点处,并在进行修改并调用刷新部分后查看文件,您将看到您的更改)。
如果您正在调试一个查找设置的程序,如果不存在,则写入它,这会非常令人困惑。即使预先警告您不要期望该设置在第二次运行程序时出现,人们也可能会期望它在第一次运行程序之后和第二次运行之前出现......唉!
没什么好担心的,因为正如其他人已经说过的那样,当应用程序部署或直接从 bin 启动时,这一切都可以工作......
但是,如果您正在调试程序并决定使用,则可能会陷入“陷阱”第一次应用程序设置,为了避免手写 XML,您决定从代码开始并让程序编写一个设置...以获得所有这些内容,然后可能会添加更多内容。
Further to Appetere's comment on the second answer:
Also note that if you're debugging (and haven't disabled the vshost process), then when your application stops, yourexecutable.vshost.exe.config will be replaced with yourexecutable.exe.config again.
So once again, you may not see any changes you made afterwards! (If you stop at a breakpoint whilst debugging and look in the file after making your modification and calling refresh section, you'll see your changes).
This is very confusing if you are debugging a program which looks for a setting and, if not present, writes it. Even if you're forewarned against expecting the the setting to be there the second time you run the program, one might expect it to be there AFTER the first run of the program and BEFORE the second run ... alas!
It's nothing to worry about since it all just works when the application is deployed or started directly from bin as others have already stated...
But it's possible to fall into a 'trap' though if you're debugging your program and decide to use Application Settings for the first time, and to avoid hand-writing the XML you decide you'll start from code and get the program to write a setting... to get all that stuff, then maybe add several more.