如何设置和访问app.config文件啊

解决方案 »

  1.   

    app.config一般编译后成为如下的文件名格式:
    你的项目生成文件名(包含后缀名)+.config
    例如:a.exe.config
    引入命名空间
    using System.Xml;
    using System.Configuration;
    然后声明ConfigXmlDocument _configDoc,就可以像访问xml文档一样访问了
      

  2.   

    public class WebConfig:IConfig
    {
            private System.Xml.XmlDocument xmlDoc;
    private string fileName;
    private IDataSecurity iDataSecurity;
    /// <summary>
    /// 构造函数
    /// </summary>
    /// <param name="filename">配置文件名字</param>
    /// <param name="ids">加密解密类</param>
    public WebConfig(string filename,IDataSecurity ids)
    {
    xmlDoc=new XmlDocument();
    fileName=filename;
    iDataSecurity=ids;
    try
    {
    xmlDoc.Load(filename);

    }
    catch
    {
    xmlDoc=null;
    }
    }
    /// <summary>
    /// 获取相应的配置
    /// </summary>
    /// <param name="configField">配置字段</param>
    /// <returns>如果取到该值返回对应值,没有则返回null</returns>
    public string GetConfig(string configField)
    {
    // TODO:  添加 WebConfig.GetConfig 实现
    XmlNode xmlNode=xmlDoc.SelectSingleNode("configuration/appSettings/add[@key='" +configField +"']");
    if(xmlNode==null)
    {
    return null;
    }
    return iDataSecurity.Decrypt(xmlNode.Attributes["value"].Value);
    } /// <summary>
    /// 设置配置字段
    /// </summary>
    /// <param name="configField">配置字段</param>
    /// <param name="configValue">配置值</param>
    public void SetConfig(string configField, string configValue)
    {
    XmlElement xmlNode=(XmlElement)xmlDoc.SelectSingleNode("configuration/appSettings/add[@key='" +configField +"']");
    if(xmlNode==null)
    return;
    xmlNode.SetAttribute("value",iDataSecurity.Encrypt(configValue));
    xmlDoc.Save(fileName);
    }
    }
      

  3.   

    ConfigurationSettings.AppSettings("*****")
      

  4.   

    1.有appSettings节的配置文件及访问方法
    //app.config文件
    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
        <appSettings>
            <add key="connectionstring" value="User ID=sa;Data Source=.;Password=;Initial Catalog=test;Provider=SQLOLEDB.1;" />
            <add key="TemplatePATH" value="Template" />
        </appSettings>
    </configuration>//app.config访问string _connectionString=ConfigurationSettings.AppSettings["connectionstring"];
    2.自定义配置节//app.config文件<?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <configSections>
            <section name="Test1" type="System.Configuration.SingleTagSectionHandler"/>
            <section name="Test2" type="System.Configuration.DictionarySectionHandler"/>
            <section name="Test3" type="System.Configuration.NameValueSectionHandler" />
        </configSections>
        
        <Test1 setting1="Hello" setting2="World"/>
        <Test2>
            <add key="Hello" value="World" />
        </Test2>
        <Test3>
            <add key="Hello" value="World" />
        </Test3>    
    </configuration>//app.config访问
                //访问配置节Test1
                IDictionary IDTest1 = (IDictionary)ConfigurationSettings.GetConfig("Test1");
                string str = (string)IDTest1["setting1"] +" "+(string)IDTest1["setting2"];
                MessageBox.Show(str);        //输出Hello World            //访问配置节Test1的方法2
                string[] values1=new string[IDTest1.Count];
                IDTest1.Values.CopyTo(values1,0);
                MessageBox.Show(values1[0]+" "+values1[1]);    //输出Hello World
                
                //访问配置节Test2
                IDictionary IDTest2 = (IDictionary)ConfigurationSettings.GetConfig("Test2");
                string[] keys=new string[IDTest2.Keys.Count];
                string[] values=new string[IDTest2.Keys.Count];
                IDTest2.Keys.CopyTo(keys,0);
                IDTest2.Values.CopyTo(values,0);
                MessageBox.Show(keys[0]+" "+values[0]);
                
                //访问配置节Test3
                NameValueCollection nc=(NameValueCollection)ConfigurationSettings.GetConfig("Test3");
                MessageBox.Show(nc.AllKeys[0].ToString()+" "+nc["Hello"]);    //输出
      

  5.   

    public static string ReadXml(string key)
    {
    string returnValue="";
    try
    {
    returnValue=System.Configuration .ConfigurationSettings .AppSettings[key];
    }
    catch(Exception e)
    {
    MessageBox.Show (e.Message .ToString ());
    }
    return returnValue;
    }
    public static void WriteXml(string key,string key_value)
    {
    XmlDocument dc=new XmlDocument ();
    dc.Load (filePath);

    XmlNode node=dc.SelectSingleNode ("//appSettings");
    if(node==null)
    {
    throw new InvalidOperationException ("Can't find \"appSettings\" Keys in the config file.");
    }
    try
    {
    XmlElement elem=(XmlElement)node.SelectSingleNode (string.Format ("//add[@key='{0}']",key));
    if(elem!=null)
    {
    elem.SetAttribute ("value",key_value);
    }
    else
    {
    elem=dc.CreateElement ("add");
    elem.SetAttribute ("key",key);
    elem.SetAttribute ("value",key_value);
    node.AppendChild (elem);
    }
    dc.Save (filePath);
    }
    catch
    {
    throw;
    }
    }