Storing data into appconfig from textbox

末鹿安然 提交于 2019-12-06 06:00:45

You can update the configuration file with the path value passed from txt box as follows,

Note : When you test this method in debug mode in visual studio you will see only AppConfig.vshost.exe.config will be updated witht the value passed.

private static void UpdateConnectionString(string path)
{
        Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
        configuration.AppSettings.Settings["ConfigurationKeyForPath"].Value = path;

        //Save only the modified section of the config
        configuration.Save(ConfigurationSaveMode.Modified);

        //Refresh the appSettings section to reflect updated configurations
        ConfigurationManager.RefreshSection("appSettings");           
}

I do not recommend at all to save user input in the app.config. This is not the purpose of the file. It is used for you as a developer or an operator to configure your application. If the database settings are provided by a user (I presume this is an administrator - not any user should be able to change database settings of your application...) you can store them in a database or in a file different than the app.config.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!