C# app.config 配置文件的读取方法

不同的应用程序有不同的读取方法 ,有可能是不同 的.NET版本语法造成的;具体原因暂时没有完全搞懂。

第一种方法:使用System.Configuration.ConfigurationSettings.AppSettings  来读取;

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
    <appSettings>
        <add key="connstring" value="Data Source=.;Initial Catalog=bookshop;User ID=sa;pwd=li;Integrated Security=TRUE"/>
    </appSettings>
</configuration>

 

 1         public static DataTable ExecuteQuery(string sql)
 2         {
 3             string connstring = System.Configuration.ConfigurationSettings.AppSettings["connstring"].ToString();
 4             conn = new SqlConnection(connstring);
 5 
 6             SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
 7             DataTable dt = new DataTable();
 8             //sda.SelectCommand = cmd;
 9             sda.Fill(dt);
10             return dt;
11         }

第二种方法 :

 1 <?xml version="1.0"?>
 2 <configuration>
 3     <configSections>
 4         <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
 5             <section name="SunEast.WMS.WinAPP.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
 6         </sectionGroup>
 7         <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
 8             <section name="SunEast.WMS.WinAPP.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false"/>
 9         </sectionGroup>
10     </configSections>
11    
12   <applicationSettings>
13     <SunEast.WMS.WinAPP.Properties.Settings>
14       <setting name="LogPath" serializeAs="String">
15         <value>\\Log</value>
16       </setting>
17       <setting name="IsLogTracking" serializeAs="String">
18         <value>True</value>
19       </setting>
20       <setting name="UserPwd" serializeAs="String">
21         <value>123456</value>
22       </setting>
23       <setting name="ReportPath" serializeAs="String">
24         <value>\\Frx</value>
25       </setting>
26       <setting name="DBString" serializeAs="String">
27         <value>Persist Security Info =true; Password=lini;User ID = sa; Initial Catalog = MS; Data Source =127.0.0.1;Connect Timeout=10</value>
28       </setting>
29     </SunEast.WMS.WinAPP.Properties.Settings>
30   </applicationSettings>
31   <userSettings>
32     <SunEast.WMS.WinAPP.Properties.Settings>
33       <setting name="IsShow" serializeAs="String">
34         <value>true</value>
35       </setting>
36     </SunEast.WMS.WinAPP.Properties.Settings>
37   </userSettings>
38 <startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>

 

C# app.config 配置文件的读取方法

 

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace APP
{
    public class AppConst
    {
        public static string DBConnstring = Properties.Settings.Default.DBString;

        public static string ReportPath = System.IO.Directory.GetCurrentDirectory() + Properties.Settings.Default.ReportPath;

        public static string LogPath = System.IO.Directory.GetCurrentDirectory() + Properties.Settings.Default.LogPath;

        public static bool IsLogTracking = Properties.Settings.Default.IsLogTracking;
    }
}

 

上一篇:10.19 模拟赛题解报告


下一篇:寒假集训三补题与题解