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;

 

 

posted @ 2019-08-29 20:06  FredGrit  阅读(239)  评论(0编辑  收藏  举报