I have a solution with one asp.net-mvc-2 project, one web service, one class library and three normal desktop apps. Currently I need to copy all app.config settings from one project to the other. That is a nightmare if I want to change something.
I know there is a solution to store all these information in the global .config files, but I am looking for a solution to store the values dedicated to my solution not to my machine. Is that possible?
Web. Config is used for asp.net web projects / web services. App. Config is used for Windows Forms, Windows Services, Console Apps and WPF applications.
The Web. Config file is used to configure Oracle Web Application functionality. This file is typically installed in the c:\Inetput\wwwroot\WebApp directory.
A configuration file (web. config) is used to manage various settings that define a website. The settings are stored in XML files that are separate from your application code. In this way you can configure settings independently from your code. Generally a website contains a single Web.
This article describes the syntax of configuration files and provides information about the three types of configuration files: machine, application, and security.
You can add a single .config file to one of your projects, and then choose Add -> Existing Item... -> Add As Link to add a reference to that file to your other projects. They will all build with the file as if it was their own copy, but there will really only be one copy, and you will only have to make changes in a single place.
I have found a really simple solution here:
1. Create a file CommonSettings.config
In your Common, Class library project.
2. Put your common setting in this file:
<appSettings>
<add key="someCommonSetting" value="some value"></add>
<!-- more setting here -->
</appSettings>
Note: <appSetting>
has to be the root element in your CommonSettings.config
(don't put it under <configuration>
)
3. Make sure CommonSettings.config
file is copied to output directory:
4. In all other project's App.Config/Web.config files, add the above common settings
That's it... You common settings will be included in every other config file.
<appSettings file="CommonSettings.config">
<add key="Value1" value="123" />
</appSettings>
Note:
For this approach to work, the shared config file should be copied to the project’s output directory so it is adjacent to the regular App/Web.config file. Add the existing shared .config file to the project as a Linked file and set it to ‘Copy if newer’. You should see something similar to this in your .csproj file:
<None Include="..\CommonConnectionStrings.config"> <Link>CommonConnectionStrings.config</Link> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> </None>
You can do the same for ConnectionString
, copy all your connection strings in CommonConnectionStrings.config
, and in other App.Config/Web.Config, reference it like this:
<connectionStrings configSource="CommonConnectionStrings.config" />
Note: If you are using this solution for connectionStrings
, you cannot have a project specific connection string, all of the connection strings will be copied from the common config.
Assuming you could have a path where both your website and webservice could access it, then you could potentially use the appsettings " file " attribute to have the settings stored in one common place. Refer http://www.codeproject.com/KB/dotnet/appsettings_fileattribute.aspx Hope this helps!
I would comment on the answer @Sahuagin gave, but I don't have enough reputation. This does work with App.config files, but you have to add a text file, then call it App.config. After that just declare the XML (as seen below)
<configuration>
<!-- add you values or whatever here-->
</configuration>
Then do as @Sahuagin said and add it as a link to your solution. It works a treat.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With