发布时间:2023-05-26 12:30
应用程序配置文件,对于asp.net是 web.config,对于WINFORM程序是 App.Config(ExeName.exe.config)。
配置文件,对于程序本身来说,就是基础和依据,其本质是一个xml文件,对于配置文件的操作,从.NET 2.0 开始,就非常方便了,提供了 System [.Web] .Configuration 这个管理功能的命名空间,要使用它,需要添加对 System.configuration.dll的引用。
System.Configuration.ConfigurationManager.AppSettings[“y”];
Asp.Net程序:
System.Web.Configuration.WebConfigurationManager.AppSettings[“y”];
读取最新值:
Configuration config = ConfigurationManager.OpenExeConfiguration(null); AppSettingsSection app = config.AppSettings; // 或者AppSettingsSection app =config.GetSection(\"AppSettings\") as AppSettingsSection
Asp.Net程序读取值:
Configuration config = WebConfigurationManager.OpenWebConfiguration(null); AppSettingsSection app = config.AppSettings; // 或者AppSettingsSection app =config.GetSection(\"AppSettings\") as AppSettingsSection;
string y=app.Settings[\"y\"].Value; foreach (string key in app.Settings) { Console.WriteLine(key+\",\"+ app.Settings[key].Value); }
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); AppSettingsSection app = config.AppSettings; app.Settings.Add(\"x\", \"this is X\"); config.Save(ConfigurationSaveMode.Modified);
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); AppSettingsSection app = config.AppSettings; //app.Settings.Add(\"x\", \"this is X\"); app.Settings[\"x\"].Value = \"this is not Y\"; config.Save(ConfigurationSaveMode.Modified);
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); AppSettingsSection app = config.AppSettings; app.Settings.Remove(\"x\"); config.Save(ConfigurationSaveMode.Modified);
说明:需要注意的是,代码所修改的并不是app.config,而是[Application_Name].exe.config这个文件。
其中Application_Name就是你的可执行文件的文件名,而[Application_Name].exe.config才是真正起作用的配置文件。
至于app.config,把它理解为是初始化配置文件比较合适。
ConnectionStringSettings conn=ConfigurationManager.ConnectionStrings[\"y\"];
或者
Configuration config = ConfigurationManager.OpenExeConfiguration(null); ConnectionStringsSection connSeciton = config.ConnectionStrings; connSeciton.ConnectionStrings.Add(new ConnectionStringSettings(\"name\", \"connectionstring\", \"provider\"));
public static void EncryptConnectionString(bool encrypt) { Configuration configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); ConnectionStringsSection configSection = configFile.GetSection(\"connectionStrings\") as ConnectionStringsSection; if ((!(configSection.ElementInformation.IsLocked)) && (!(configSection.SectionInformation.IsLocked))) { if (encrypt && !configSection.SectionInformation.IsProtected) //encrypt is false to unencrypt { configSection.SectionInformation.ProtectSection(\"DataProtectionConfigurationProvider\"); } if (!encrypt && configSection.SectionInformation.IsProtected) { configSection.SectionInformation.UnprotectSection(); //encrypt is true so encrypt } configSection.SectionInformation.ForceSave = true; //re-save the configuration file section configFile.Save(); // Save the current configuration. } }
到此这篇关于C#操作配置文件app.config、web.config增删改的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持脚本之家。