c#读取配置文件 程序的App.config

读取配置文件,首先得引入configuration.dll

如:using System.Configuration;

1. 配置文件app.config---appSettings


 
   
   
   
 


C# 源代码读取:

        string dbaUserName = ConfigurationManager.AppSettings["dbaUserName"];  //以key获取appSettings字符串
        string dbaUserPwd = ConfigurationManager.AppSettings["dbaUsePwd"];
        string oracleServerName = ConfigurationManager.AppSettings["oracleServerName"];

 

2. 配置文件app.config---connectionStrings

 
             connectionString="user Id=sys;Password=sys;Server=127.0.0.1;Direct=true;Sid=orcl;Persist Security Info=True"
         providerName="using Devart.Data.Oracle;"/>
 


C# 源代码读取:

string connStr = ConfigurationManager.ConnectionStrings["connectionString"].ToString();   //以name获取ConnectionStrings字符串

3. 配置文件不是以app.config命名,而是以其他名称命名,如connection.cfg


   
   
   
 

 

C# 源代码读取:

            System.Xml.XmlDocument xDoc = new XmlDocument();
            string strPath = System.IO.Directory.GetCurrentDirectory();
            xDoc.Load(System.IO.Directory.GetCurrentDirectory() + "\\connection.cfg");
            System.Xml.XmlNodeList xList = xDoc.GetElementsByTagName("oracle");
            int len = xList.Count;
            for (int i = 0; i < len; i++)
            {
                string name = xList.Item(i).Attributes.GetNamedItem("name").Value;
           }

 

你可能感兴趣的:(综合)