使用同一App在执行时编辑app.config [英] Editing app.config in execution time using the same App

查看:79
本文介绍了使用同一App在执行时编辑app.config的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用app.config的Windows Forms应用程序VS 2008-C#.

I have an Windows Forms application VS 2008 - C#, that uses app.config.

在执行时,我要在应用程序的菜单"选项中编辑app.config的值,将其保存并重新启动应用程序.

In execution time, in Menu option of my application, I want editing values of app.config, save it and restart application.

任何示例源代码,任何良好的模式和做法??

any sample source code, any good patterns and practices ??

在MSDN论坛中,Jean Paul VA:

in MSDN Forums, Jean Paul VA:

  1. 创建一个测试Windows窗体应用程序,并将app.config添加到其中.

  1. Create an test windows forms application and add an app.config into it.

添加对System.confguration的引用

Add reference to System.confguration

在appSettings中添加一个名为"font"的键,其值为"Verdana"

Add a key named "font" in appSettings with value "Verdana"

在表单上放置一个按钮,并单击它添加修改代码.

Place a button on form and on click of it add the modification code.

    System.Configuration.Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

    configuration.AppSettings.Settings.Remove("font");
    configuration.AppSettings.Settings.Add("font", "Calibri");

    configuration.Save(ConfigurationSaveMode.Modified);

    ConfigurationManager.RefreshSection("appSettings");

您怎么看?

推荐答案

我认为您实际上无法在运行时写入配置文件-它很可能是只读的,但是,可能有解决方法通过重写文件(根据需要进行适当的更改)并基本上替换现有的文件,然后进一步重新启动应用程序以加载新值(我非常怀疑这样做是否可取,并且个人不会尝试使用此恶意代码).

I don't think you can actually write to the configuration file at runtime - it may well be read-only, however, there may be a way around this by re-writing the file (with proper alterations as required) and essentially replacing the existing one, then, further, restarting the application to load the new values (I highly doubt this is desirable and personally would not try to instrument this malarkey).

您也可以考虑将应用程序设置存储在设置文件中,以这种方式轻松进行操作.

You may also just consider storing application settings in the settings file which are easily manipulated in this way.

要使用设置,首先假设您有一个Settings.settings文件(如果没有,则创建一个:Add Item-> Settings File),然后我们配置一个名为MyUnicornsName的设置.为了进行更改并坚持下去,您只需执行以下操作即可:

To use settings, first let's assume you have a Settings.settings file (if not then create one: Add Item->Settings File), then we have a setting configured named MyUnicornsName; In order to make a change and persist it you can simply do as follows:

Settings.Default.MyUnicornsName = "Lucifers Spawn";
Settings.Default.Save();

类似地,要读取设置:

ATextDisplayControl.Text = Settings.Default.MyUnicornsName

如果您不知道,当您在IDE中打开/双击设置文件时,Visual Studio将打开设置编辑器.使用此界面,您可以添加和编辑初始设置及其值,并且string并不是唯一受支持的值,可以使用所有原语,而且据我所知,也可以使用任何可序列化的值.

In case you don't know, Visual Studio will open the settings editor when you open/double click the settings file in the IDE; using this interface you can add and edit your initial settings and their values, and string is not the only supported value, all primitives can be used, and, as far as I know, any serializable value too.

这篇关于使用同一App在执行时编辑app.config的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆