Reputation: 65
I have one problem. I have made solution and it is 3-tier application. So I have 3 projects that are class libraries and one is windows form application which will use me to start all of that and connect them together. Problem is I have for data layer app.config file. If I put it in that dll then I can't read it. If I put it in main program (windows form application) I am still not able to read it.
So my question is where to put app.config file so I can read it normally? I am reading it from DLL. It has to be that way because it is 3.tier app and all layers are on their own. So one is data layer, buisness logic and presentation layer.
Please help.
EDIT:
okay I'll pass some code:
so I am reading from data layer app.config file like this:
SqlConnection konekcija = new SqlConnection(ConfigurationManager.AppSettings["skripta"]);
app.config file in that class library project is:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0"/>
</startup>
<appSettings>
<add key="skripta"
value="Data Source=DENONTH-PC;Initial Catalog=rups;Integrated Security=True;Pooling=False" />
</appSettings>
</configuration>
I have app.config in that class librabry and in Windows form project and still nothing is referencing it properly.
Upvotes: 0
Views: 6030
Reputation: 5226
Usually I have one app.config per project, but real values are kept in the main (usually UI) application's config. Then I create some Core/Utility project and there I create one class that's responsible for exposing configuration keys/values. The Core/Utility project has no dependencies, but can be (and is!) referenced by any project that needs config values. It gives me a really simple layer of abstraction if one day I would like to switch to e.g. database driven configuration.
So in the end: put everything in windows forms config. It should be absolutely reachable from the class library if using ConfigurationManager class.
Edit#1: first of all if you want to store and use connection string, do it properly, which means: in the app.config:
<connectionStrings>
<add name="ConnString" connectionString="server..."/>
</connectionStrings>
in code:
ConfigurationManager.ConnectionStrings["ConnString"];
Try now. I'm pretty sure you're doing something wrong because your code should be working.
Upvotes: 7
Reputation: 56
You can add app config file in windows application. You can access it by using ConfigurationManager class.
Refer this link also, hope this may help you.
Visit http://msdn.microsoft.com/en-us/library/ms184658%28v=vs.80%29.aspx
Upvotes: 0