C# read dll config

public static SqlConnection GetSqlConnection()
        {
            Configuration myDllConfig =
            ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().Location);
            // Get the appSettings section
            AppSettingsSection myDllConfigAppSettings =(AppSettingsSection)myDllConfig.GetSection("appSettings");
            string connString = myDllConfigAppSettings.Settings["SQLConnString"].Value;
            SqlConnection connection = new SqlConnection(connString);
            return connection;
        }

 1.Right click on the *.config file in the dll project,then select Properties,as the below snapshot illustrates.

 2.Set 'Copy to Output Directory' as 'Copy Always';

 3.Rebuild Dll project and rebuild the Solution.

Configuration myDllConfig =
ConfigurationManager.OpenExeConfiguration(this.GetType().Assembly.Location);
// Get the appSettings section
AppSettingsSection myDllConfigAppSettings =
(AppSettingsSection)myDllConfig.GetSection("appSettings");

string connString = myDllConfigAppSettings.Settings["SQLConnString"].Value;

原文地址:https://www.cnblogs.com/Fred1987/p/11431731.html