简体   繁体   English

如何在C#Windows应用程序中以编程方式修改设置文件(app.config)的信息?

[英]How to modify the information of the Settings file(app.config) programatically in C# windows Application?

We are delveloping C# windows based application .net 4 environment.In My application, i want to update settings file(app.config) attribute programatically.Actually we created one user interface(textbox).So, User will enter the new configuration information in the textbox.When clicking the save button, i want to update this value in settings file permanently. 我们正在开发基于C#Windows的应用程序.net 4环境。在我的应用程序中,我想以编程方式更新设置文件(app.config)属性。实际上我们创建了一个用户界面(文本框),因此,用户将在其中输入新的配置信息单击保存按钮时,我要在设置文件中永久更新此值。

for example: 例如:

I have Encryption Key in my settings file.Suppose, user may enter the new encryption key using that interface,then it will automatically update the settings file Encryption key value. 我的设置文件中有加密密钥。假设用户可以使用该界面输入新的加密密钥,那么它将自动更新设置文件的加密密钥值。

Is this possible? 这可能吗?

First of all you have to add this reference : System.Configuration , then you can write to app.config for example with this code : 首先,您必须添加以下引用: System.Configuration ,然后可以使用以下代码例如写入app.config

        Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

        ConfigurationSectionGroup applicationSectionGroup = config.GetSectionGroup("applicationSettings");
        ConfigurationSection applicationConfigSection = applicationSectionGroup.Sections["YourApp.Properties.Settings"];
        ClientSettingsSection clientSection = (ClientSettingsSection)applicationConfigSection;

        //Encryption Key Configuration Setting
        SettingElement applicationSetting = clientSection.Settings.Get("EncryptionKey");
        applicationSetting.Value.ValueXml.InnerXml = this.textBoxKey.Text.Trim();

        applicationConfigSection.SectionInformation.ForceSave = true;
        config.Save();

        ConfigurationManager.RefreshSection("applicationSettings");

Bear in mind that you have to create the app.config file before with the Visual studio wizard, in this example i have called the field for the encryption key : EncryptionKey and the related textbox in the form : textBoxKey . 请记住,您必须先使用Visual Studio向导创建app.config文件,在本示例中,我已经调用了加密密钥字段: EncryptionKey和相关文本框,格式为: textBoxKey

The best way to edit the app.config is by creating a setup project. 编辑app.config的最佳方法是创建一个安装项目。

A setup project will run as Admin and will be allowed to write in the program files folder. 安装项目将以Admin身份运行,并且将被允许写入程序文件文件夹中。 Writing to the program files folder by regular, standard users is not allowed. 不允许普通的标准用户写入程序文件文件夹。

During the setup you could gather information from the users about values that should be set in the config file and you could search for the correct folder. 在安装过程中,您可以从用户那里收集有关应在配置文件中设置的值的信息,并且可以搜索正确的文件夹。

Note that if you want to change the per user settings you will not run into this issue because the user setting are written in the profile of the user. 请注意,如果要更改每个用户的设置,则不会遇到此问题,因为用户设置写在用户的配置文件中。

This may help 这可能会有所帮助

        Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
        AppSettingsSection appsettings = (AppSettingsSection)config.GetSection("appSettings");
        appsettings.Settings["Key"].Value = "value";
        config.Save(ConfigurationSaveMode.Modified);

Do you want to edit the config file at runtime? 是否要在运行时编辑配置文件? This is possible. 这个有可能。 Use the ConfigurationManager class from the System.Configuration assembly (add reference to it). 使用System.Configuration程序集中的ConfigurationManager类(添加对它的引用)。 You an get any section you like eg appSettings and modify them. 您会得到自己喜欢的任何部分,例如appSettings并对其进行修改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM