ConfigurationManager 不保存设置

发布于 2024-10-03 04:08:49 字数 567 浏览 1 评论 0原文

这是我正在使用的代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

绾颜 2024-10-10 04:08:49

我认为您应该调用 Save 方法

ConfigurationManager.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");

编辑

为了能够保存,您必须使用 OpenExeConfiguration 方法返回的配置对象

//Create the object
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

//make changes
config.AppSettings.Settings["Username"].Value = txtUsername.Text;
config.AppSettings.Settings["Password"].Value = txtPassword.Text;

//save to apply changes
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");

更多参考此处ConfigurationManager 类

I think you should call the Save method

ConfigurationManager.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");

EDIT

To be able to save you have to use a configuration object returned by the OpenExeConfiguration Method

//Create the object
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

//make changes
config.AppSettings.Settings["Username"].Value = txtUsername.Text;
config.AppSettings.Settings["Password"].Value = txtPassword.Text;

//save to apply changes
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");

More references here ConfigurationManager Class

深爱不及久伴 2024-10-10 04:08:49

当您使用 F5 运行应用程序时,

  • 您的代码将被编译,
  • 可执行文件将被复制到源代码目录的 binbin\Debug 子目录中,
  • 您的 app .config 将作为 yourexecutable.exe.config 复制到该目录中,并且
  • 您的可执行文件将在该目录中启动。

因此,您的应用程序使用 binbin\Debug 目录中的 yourexecutable.exe.config,并且它位于那里 ConfigurationManager 保存更改,而不是在源代码目录中。部署应用程序后,这不会成为问题,因为这样,更改将转到部署目录中的yourexecutable.exe.config,这正是您想要的。

When you run your application with F5,

  • your code is compiled,
  • the executable is copied to the bin or bin\Debug subdirectory of your source code directory,
  • your app.config is copied as yourexecutable.exe.config into that directory, and
  • your executable is started in that directory.

Thus, your application uses the yourexecutable.exe.config in the bin or bin\Debug directory, and it is there that ConfigurationManager saves the changes, not in your source code directory. This won't be an issue after deploying your application, because then, changes will go to yourexecutable.exe.config in the deployment directory, which is what you want.

两相知 2024-10-10 04:08:49

根据 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文