操作App.config与Web.config文件

时间:2022-11-16 11:36:21

对于动态操作配置文件我想大家都不陌生,以前都是用的操作xml的方式,在.net2.0以上版本我可以利用新的方法来实现

自己对自己的配置:

操作App.config与Web.config文件public   class  ConfigureAppConfig
操作App.config与Web.config文件操作App.config与Web.config文件    
{
操作App.config与Web.config文件        
//静态构造,不能实例化
操作App.config与Web.config文件操作App.config与Web.config文件
        static ConfigureAppConfig() { }
操作App.config与Web.config文件
操作App.config与Web.config文件操作App.config与Web.config文件        
/// <summary>
操作App.config与Web.config文件        
/// 获取AppSettings配置节中的Key值
操作App.config与Web.config文件        
/// </summary>
操作App.config与Web.config文件        
/// <param name="keyName">Key's name</param>
操作App.config与Web.config文件        
/// <returns>Key's value</returns>

操作App.config与Web.config文件        public static string GetAppSettingsKeyValue(string keyName)
操作App.config与Web.config文件操作App.config与Web.config文件        
{
操作App.config与Web.config文件            
return ConfigurationManager.AppSettings.Get(keyName);
操作App.config与Web.config文件        }

操作App.config与Web.config文件
操作App.config与Web.config文件操作App.config与Web.config文件        
/// <summary>
操作App.config与Web.config文件        
/// 获取ConnectionStrings配置节中的值
操作App.config与Web.config文件        
/// </summary>
操作App.config与Web.config文件        
/// <returns></returns>

操作App.config与Web.config文件        public static string GetConnectionStringsElementValue()
操作App.config与Web.config文件操作App.config与Web.config文件        
{
操作App.config与Web.config文件            ConnectionStringSettings settings 
= System.Configuration.ConfigurationManager.ConnectionStrings["connectionString"];
操作App.config与Web.config文件            
return settings.ConnectionString;
操作App.config与Web.config文件        }

操作App.config与Web.config文件
操作App.config与Web.config文件操作App.config与Web.config文件        
/// <summary>
操作App.config与Web.config文件        
/// 保存节点中ConnectionStrings的子节点配置项的值
操作App.config与Web.config文件        
/// </summary>
操作App.config与Web.config文件        
/// <param name="elementValue"></param>

操作App.config与Web.config文件        public static void ConnectionStringsSave(string ConnectionStringsName, string elementValue)
操作App.config与Web.config文件操作App.config与Web.config文件        
{
操作App.config与Web.config文件            System.Configuration.Configuration config 
= ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
操作App.config与Web.config文件            config.ConnectionStrings.ConnectionStrings[
"connectionString"].ConnectionString = elementValue;
操作App.config与Web.config文件            config.Save(ConfigurationSaveMode.Modified);
操作App.config与Web.config文件            ConfigurationManager.RefreshSection(
"connectionStrings");
操作App.config与Web.config文件        }

操作App.config与Web.config文件
操作App.config与Web.config文件操作App.config与Web.config文件        
/// <summary>
操作App.config与Web.config文件        
/// 判断appSettings中是否有此项
操作App.config与Web.config文件        
/// </summary>

操作App.config与Web.config文件        private static bool AppSettingsKeyExists(string strKey, Configuration config)
操作App.config与Web.config文件操作App.config与Web.config文件        
{
操作App.config与Web.config文件            
foreach (string str in config.AppSettings.Settings.AllKeys)
操作App.config与Web.config文件操作App.config与Web.config文件            
{
操作App.config与Web.config文件                
if (str == strKey)
操作App.config与Web.config文件操作App.config与Web.config文件                
{
操作App.config与Web.config文件                    
return true;
操作App.config与Web.config文件                }

操作App.config与Web.config文件            }

操作App.config与Web.config文件            
return false;
操作App.config与Web.config文件        }

操作App.config与Web.config文件
操作App.config与Web.config文件操作App.config与Web.config文件        
/// <summary>
操作App.config与Web.config文件        
/// 保存appSettings中某key的value值
操作App.config与Web.config文件        
/// </summary>
操作App.config与Web.config文件        
/// <param name="strKey">key's name</param>
操作App.config与Web.config文件        
/// <param name="newValue">value</param>

操作App.config与Web.config文件        public static void AppSettingsSave(string strKey, string newValue)
操作App.config与Web.config文件操作App.config与Web.config文件        
{
操作App.config与Web.config文件            System.Configuration.Configuration config 
= ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
操作App.config与Web.config文件            
if (AppSettingsKeyExists(strKey, config))
操作App.config与Web.config文件操作App.config与Web.config文件            
{
操作App.config与Web.config文件                config.AppSettings.Settings[strKey].Value 
= newValue;
操作App.config与Web.config文件                config.Save(ConfigurationSaveMode.Modified);
操作App.config与Web.config文件                ConfigurationManager.RefreshSection(
"appSettings");
操作App.config与Web.config文件            }

操作App.config与Web.config文件        }

操作App.config与Web.config文件    }

如果你的程序是对其它程序的配置文件进行操作,代码如下:
操作App.config与Web.config文件            ExeConfigurationFileMap filemap  =   new  ExeConfigurationFileMap();
操作App.config与Web.config文件            filemap.ExeConfigFilename 
=  filePath; // 配置文件路径
操作App.config与Web.config文件
            config  =  ConfigurationManager.OpenMappedExeConfiguration(filemap, ConfigurationUserLevel.None);
操作App.config与Web.config文件
操作App.config与Web.config文件            
if  (AppSettingsKeyExists( " Refresh " , config))
操作App.config与Web.config文件操作App.config与Web.config文件            
{
操作App.config与Web.config文件                config.AppSettings.Settings[
"Refresh"].Value = M_TimeRead.ToString();
操作App.config与Web.config文件            }

操作App.config与Web.config文件
操作App.config与Web.config文件            
if  (AppSettingsKeyExists( " MachineNo " , config))
操作App.config与Web.config文件操作App.config与Web.config文件            
{
操作App.config与Web.config文件                config.AppSettings.Settings[
"MachineNo"].Value = M_MachineNo;
操作App.config与Web.config文件
操作App.config与Web.config文件            }

操作App.config与Web.config文件            config.Save(ConfigurationSaveMode.Modified);
操作App.config与Web.config文件            ConfigurationManager.RefreshSection(
" appSettings " );
操作App.config与Web.config文件
操作App.config与Web.config文件            config.ConnectionStrings.ConnectionStrings[
" connectionString " ].ConnectionString  =  M_ConnectionString;
操作App.config与Web.config文件            config.Save(ConfigurationSaveMode.Modified);
操作App.config与Web.config文件            ConfigurationManager.RefreshSection(
" connectionStrings " );

数据库字符串加密
操作App.config与Web.config文件ExeConfigurationFileMap filemap  =   new  ExeConfigurationFileMap();
操作App.config与Web.config文件            filemap.ExeConfigFilename 
=  Application.ExecutablePath  +   " .Config " // filePath;
操作App.config与Web.config文件
            config  =  ConfigurationManager.OpenMappedExeConfiguration(filemap, ConfigurationUserLevel.None);
操作App.config与Web.config文件            
// 指定我所要的节点
操作App.config与Web.config文件
            ConfigurationSection section  =  config.ConnectionStrings;
操作App.config与Web.config文件
操作App.config与Web.config文件            
if  ((section.SectionInformation.IsProtected  ==   false &&  (section.ElementInformation.IsLocked  ==   false ))
操作App.config与Web.config文件操作App.config与Web.config文件            
{
操作App.config与Web.config文件                
//制定节点加密
操作App.config与Web.config文件
                section.SectionInformation.ProtectSection(protect);
操作App.config与Web.config文件                
//即使没有修改也保存设置
操作App.config与Web.config文件
                section.SectionInformation.ForceSave = true;
操作App.config与Web.config文件                
//配置文件内容保存到xml
操作App.config与Web.config文件
                config.Save(ConfigurationSaveMode.Full);
操作App.config与Web.config文件            }

 

 

 

http://www.cnblogs.com/wangsu/archive/2008/02/25/1081226.html

 

读取web.config的最好用
Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
其他一样。