使用同一应用程序在执行时编辑app.config
我有一个 Windows 窗体应用程序 VS 2008 - C#,它使用 app.config。
在执行时,在我的应用程序的菜单选项中,我想要编辑 app.config 的值,保存它并重新启动应用程序。
任何示例源代码,任何好的模式和实践?
编辑:
在 MSDN 论坛中,Jean Paul VA:
创建一个测试 Windows 窗体应用程序并向其中添加一个 app.config。
添加对 System.confguration 的引用
在 appSettings 中添加一个名为“font”的键,值为“Verdana”
在表单上放置一个按钮,然后单击它添加修改代码。
System.Configuration.Configuration 配置 = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); 配置.AppSettings.Settings.Remove(“字体”); 配置.AppSettings.Settings.Add("字体", "Calibri"); 配置.保存(ConfigurationSaveMode.Modified); ConfigurationManager.RefreshSection(“appSettings”);
你对此有何看法?
I have an Windows Forms application VS 2008 - C#, that uses 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 ??
edit:
in MSDN Forums, Jean Paul VA:
Create an test windows forms application and add an app.config into it.
Add reference to System.confguration
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");
what you think about it ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为您实际上无法在运行时写入配置文件 - 它很可能是只读的,但是,可能有一种方法可以通过重写文件(根据需要进行适当的更改)并基本上替换现有的,然后,进一步重新启动应用程序以加载新值(我非常怀疑这是可取的,并且我个人不会尝试检测这个胡言乱语)。
您也可以考虑将应用程序设置存储在设置文件中,这样可以轻松操作。
要使用设置,首先假设您有一个 Settings.settings 文件(如果没有,则创建一个:添加项目 -> 设置文件),然后我们配置一个名为
MyUnicornsName
的设置;为了进行更改并保留它,您可以简单地执行以下操作:同样,要读取设置:
如果您不知道,当您在 IDE 中打开/双击设置文件时,Visual Studio 将打开设置编辑器;使用此界面,您可以添加和编辑初始设置及其值,并且
string
不是唯一受支持的值,所有原语都可以使用,而且据我所知,也可以使用任何可序列化的值。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.
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:Similarly, to read a setting:
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.有什么原因不能使用通常的自动生成的 Properties.Settings 将更改的数据存储在设置文件中吗?一件很棒的事情是您知道自己正在更改什么,因此您甚至不必重新启动应用程序!
在 C# 中使用设置
的运行时访问设置就像这样简单:(
没有好的模式可以简单地修改 app.config 本身,因为它被设计为在该上下文中为只读,具有专家用户设置。)
Is there any reason you can't use the usual auto-gen'd Properties.Settings to store the changing data in a settings file instead? One great thing is that you know what you're changing so you don't even have to restart the application!
Using Settings in C#
Runtime access of settings is as easy as:
(There is no good pattern for modifing app.config itself simply b/c it's designed to be readonly in that context, with expert-user settings.)
使用“项目”->“属性”->“设置”来执行此类操作。
Use the Project->Properties->Settings for these kinds of things.
实际上 App.config 中的属性是只读的,所以你不能这样做。
但是有一个技巧..............................
在 Settings.cs 文件中创建一个公共的函数或方法,以便它可以可使用
Properties.settings
并编写以下代码。
记住将属性名称的确切字符串传递给方法。或者更好地为设置创建一个 Writeonly 属性。
更新
这里是设置为属性的代码,我以连接字符串为例,我打赌它可以是任何东西。请记住存储的属性是
Object
类型,因此您可以创建特定于该类型的属性。现在您可以轻松地使用此属性在运行时更改 ConnectionString...
well actually the Properties in the App.config are ReadOnly so u cant do it.
But there's a trick............................
In the Settings.cs file create a Function or method that is public so that it can be available with
Properties.settings
and write the following code..
remember to pass the exact string of the property name to the method. or better create a Writeonly Property for the setting.
update
here is a code for setting as a property, i am taking a connection string as an example, bet it can be anything. remember the property stored is of
Object
type so you can create property specific to that..Now you can easily use this Property to change the ConnectionString at run time...