惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Tenable Blog
Last Week in AI
Last Week in AI
P
Proofpoint News Feed
Engineering at Meta
Engineering at Meta
H
Help Net Security
F
Fortinet All Blogs
MyScale Blog
MyScale Blog
宝玉的分享
宝玉的分享
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 司徒正美
量子位
N
Netflix TechBlog - Medium
Apple Machine Learning Research
Apple Machine Learning Research
小众软件
小众软件
Recorded Future
Recorded Future
博客园 - 三生石上(FineUI控件)
Vercel News
Vercel News
aimingoo的专栏
aimingoo的专栏
I
InfoQ
Microsoft Security Blog
Microsoft Security Blog
Scott Helme
Scott Helme
The Last Watchdog
The Last Watchdog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
IT之家
IT之家
AI
AI
WordPress大学
WordPress大学
Security Archives - TechRepublic
Security Archives - TechRepublic
Google Online Security Blog
Google Online Security Blog
U
Unit 42
V2EX - 技术
V2EX - 技术
MongoDB | Blog
MongoDB | Blog
Schneier on Security
Schneier on Security
博客园 - Franky
H
Heimdal Security Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Jina AI
Jina AI
W
WeLiveSecurity
P
Privacy & Cybersecurity Law Blog
Cloudbric
Cloudbric
B
Blog RSS Feed
N
News | PayPal Newsroom
S
Securelist
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
I
Intezer
Hacker News - Newest:
Hacker News - Newest: "LLM"
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
博客园_首页
罗磊的独立博客
H
Hackread – Cybersecurity News, Data Breaches, AI and More
雷峰网
雷峰网

博客园 - AGPSky

资源文件配置和使用 正则表达式30分钟入门教程(第二版) C#命名规则、开发习惯和风格 Linq to SQL vs2005和vs2008默认建立web site和web application 生成的webform源码的一点区别 - AGPSky 安装VS2005 SP1时失败(错误 1718。文件被数字签名策略拒绝) gridview 增加行,进行数据汇总 - AGPSky - 博客园 利用DAAB 获取存储过程返回值的方法 - AGPSky - 博客园 【转】理解委托与事件的好文章 【转】C#委托,事件理解入门 (译稿) 【转】C# 中的委托和事件 【转】浅析C#的事件处理和自定义事件[object sender , EventArgs e] [转】object sender,EventArgs e的一些讲解 js的prototype的例子三 js的prototype的例子二 js的prototype的例子一 把控件动态添加到页面中 petshop4.0网上资料 Webdiyer的分页控件+通用存储过程+查询+ajax分页
操作App.config与Web.config文件
AGPSky · 2011-07-11 · via 博客园 - AGPSky

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

自己对自己的配置:

public class ConfigureAppConfig
    
{
        
//静态构造,不能实例化
        static ConfigureAppConfig() { }

        
/// <summary>
        
/// 获取AppSettings配置节中的Key值
        
/// </summary>
        
/// <param name="keyName">Key's name</param>
        
/// <returns>Key's value</returns>

        public static string GetAppSettingsKeyValue(string keyName)
        
{
            
return ConfigurationManager.AppSettings.Get(keyName);
        }


        
/// <summary>
        
/// 获取ConnectionStrings配置节中的值
        
/// </summary>
        
/// <returns></returns>

        public static string GetConnectionStringsElementValue()
        
{
            ConnectionStringSettings settings 
= System.Configuration.ConfigurationManager.ConnectionStrings["connectionString"];
            
return settings.ConnectionString;
        }


        
/// <summary>
        
/// 保存节点中ConnectionStrings的子节点配置项的值
        
/// </summary>
        
/// <param name="elementValue"></param>

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


        
/// <summary>
        
/// 判断appSettings中是否有此项
        
/// </summary>

        private static bool AppSettingsKeyExists(string strKey, Configuration config)
        
{
            
foreach (string str in config.AppSettings.Settings.AllKeys)
            
{
                
if (str == strKey)
                
{
                    
return true;
                }

            }

            
return false;
        }


        
/// <summary>
        
/// 保存appSettings中某key的value值
        
/// </summary>
        
/// <param name="strKey">key's name</param>
        
/// <param name="newValue">value</param>

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

        }

    }

如果你的程序是对其它程序的配置文件进行操作,代码如下:

            ExeConfigurationFileMap filemap = new ExeConfigurationFileMap();
            filemap.ExeConfigFilename 
= filePath;//配置文件路径
            config = ConfigurationManager.OpenMappedExeConfiguration(filemap, ConfigurationUserLevel.None);

            
if (AppSettingsKeyExists("Refresh", config))
            
{
                config.AppSettings.Settings[
"Refresh"].Value = M_TimeRead.ToString();
            }


            
if (AppSettingsKeyExists("MachineNo", config))
            
{
                config.AppSettings.Settings[
"MachineNo"].Value = M_MachineNo;

            }

            config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection(
"appSettings");

            config.ConnectionStrings.ConnectionStrings[
"connectionString"].ConnectionString = M_ConnectionString;
            config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection(
"connectionStrings");

数据库字符串加密

ExeConfigurationFileMap filemap = new ExeConfigurationFileMap();
            filemap.ExeConfigFilename 
= Application.ExecutablePath + ".Config"//filePath;
            config = ConfigurationManager.OpenMappedExeConfiguration(filemap, ConfigurationUserLevel.None);
            
//指定我所要的节点
            ConfigurationSection section = config.ConnectionStrings;

            
if ((section.SectionInformation.IsProtected == false&& (section.ElementInformation.IsLocked == false))
            
{
                
//制定节点加密
                section.SectionInformation.ProtectSection(protect);
                
//即使没有修改也保存设置
                section.SectionInformation.ForceSave = true;
                
//配置文件内容保存到xml
                config.Save(ConfigurationSaveMode.Full);
            }

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

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