Reputation: 23177
I'm using app.config files for the first time and am unsure how to proceed in a situation with project references and app.config files. Here's a description of my projects I'm working with:
Project1 (process logger) populates log table in database specified in Project1 app.config file.
Project2 (calculator) runs a calculation and populates a table in database specified in Project2 app.config file. Records log using reference to Project1.
When I reference Project1 in Project2, in my "Release" folder I do not see an app.config file for Project1. What is the best way to handle this situation? Instead of making the database configurable in an app.config file in Project1, should I instead make it a parameter for Project2 to specify (Project2.LogConnectionString is sent as a parameter to functions in Project1)? Or is there a way to make the app.config file appear in my "Release" folder?
Any help would be appreciated, I have no idea what is the right way to do this. Thanks!
Upvotes: 1
Views: 9184
Reputation: 5373
If you use default Properties.Settings (difference between app.config and settings explained here) and you have Project1 referenced in Project2, then you may do it like this:
// in Project2:
string connectionstring = NamespaceOfProject1.Properties.Settings.Default.Project1ConnString;
Upvotes: 0
Reputation: 6805
The best option for you would be to load Project1 confing inside Project2 at run time. What I would do is to place configuration string (or custom configuration section if required) inside Project2 confing. This settings would basicly just tell app where to look Project1 config.
Once you have confing path you can load it like this:
System.Configuration.ExeConfigurationFileMap configFile = new System.Configuration.ExeConfigurationFileMap();
configFile.ExeConfigFilename = "my_file.config";
config = System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(configFile, System.Configuration.ConfigurationUserLevel.None);
settings = config.AppSettings.Settings;
In this case I am accessing AppSettings from Project1.
And about custom configuration that I have mentioned, please see one of my posts, this one Custom configuration section will do the job if you will require more complex settings.
Upvotes: 0
Reputation: 8934
Configuration files are based on the startup project. Which means that when you instantiate something from proj1 in proj2 (proj1 will be a reference in proj2) you can use the app.config from proj2.
Basically, you can store all config in proj2 and use as need.
btw, change the proj names ;)
Upvotes: 3
Reputation: 174289
A .NET application has one configuration file. It usually has the name of the executable. .NET doesn't support a config file per assembly.
You should be passing the connection string from the executable to the logger project.
Upvotes: 1