在C#中,可以使用System.Configuration命名空間來讀取應用程序的配置文件。以下是一些技巧:
string settingValue = ConfigurationManager.AppSettings["settingKey"];
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
foreach (KeyValueConfigurationElement setting in config.AppSettings.Settings)
{
string key = setting.Key;
string value = setting.Value;
}
ConnectionStringsSection connectionStrings = ConfigurationManager.GetSection("connectionStrings") as ConnectionStringsSection;
foreach (ConnectionStringSettings connectionString in connectionStrings.ConnectionStrings)
{
string name = connectionString.Name;
string value = connectionString.ConnectionString;
}
string connectionString = ConfigurationManager.ConnectionStrings["connectionStringName"].ConnectionString;
以上是一些常用的讀取配置文件的技巧,可以根據實際的需求和配置文件的結構來選擇合適的方法來讀取配置。