C#配置文件config的使用

时间:2022-01-15 02:53:22

做程序的时候总会有一些参数,可能会调整,这时候一般情况下我都会写在配置文件里,这样方便一点。

配置文件的读取

<?xml version="1.0" encoding="utf-8" ?> <configuration> <connectionStrings> <add name="VAVOracleConnectionString" connectionString="Data Source=192.168.0.1/radiodb;Persist Security Info=True;User ID=viv;Password=123456"/> <add name="VAVOracleWeixinConnectionString" connectionString="Data Source=192.168.0.2/radiodb;Persist Security Info=True;User ID=viv;Password=123456"/> </connectionStrings> <appSettings> <add key ="CNETWORKID" value ="4205"/> <add key ="DATETIME" value ="14:28"/> </appSettings> </configuration>

1、最常用的是数据库的链接字符串,一般情况下,,都会写在配置文件里的connectionStrings节点下

获取数据库连接字符串:

public static string connectionString = ConfigurationManager.ConnectionStrings["VAVOracleWeixinConnectionString"].ToString();

2、用到的参数,我写在了appSettings节点下

获取参数:

string dateTime = ConfigurationManager.AppSettings["DATETIME"].ToString();