userSettings not loaded from .config unless in startup project? - c#

I've got a solution with a project layout of:
View (Startup project, app.config)
Settings (ApplicationSettings.Settings, app.config)
There in an ApplicationSettings.Settings file inside of the settings project which contains various user settings. This project also has an App.config file. This config file is mostly empty.
Now it's my understanding that the userSettings will load from the startup project config file in the output folder. Aka View.exe.config. This file (and the View app.config) contains my wanted startup user settings.
However after trying to get this working I can only get the View.exe.config changes to be loaded if the ApplicationSettings.Settings file is placed in the View project. And in no scenario is anything loaded from the Settings App.Config.
I'm aware these files are replaced when you rebuild and debug and that's not the issue. I also know that when you save the values are saved into your appData folder and there is nothing saved in there at this time.
Basically my question is can you have a settings file outside of the startup project which loads from a .config file when you run?

You need to do following things here.
Make sure that settings file get copied from other project to startup project when project builds. You can achieve this by using post build script.
Write config section or handler to read settings value from the settings file.
Configure config section handler in app.config and add config section with config source path pointing to relative path to the settings file in startup project.

Related

Copy settings from one Web.config to another except AppSettings and ConnectionStrings

Is it possible, through code, to copy settings from one Web.config file to another (except AppSettings and ConnectionStrings)?
I have a situation where a single MVC3 project has been deployed to multiple servers in different locations. There is an auto-updater on all of these that will pull in the latest version. Typically when this would run, it would only overwrite the application folders and NOT the Web.config file.
I just upgraded the project to MVC4. This changes basically everything in the Web.config file except the AppSettings and ConnectionStrings. All of the installations of this project would have slightly different values here.
How would I go about writing some code that will update the Web.config file, but preserve all of the AppSettings and ConnectionStrings?
.NET provides ways to get configuration from other config file. Refer this article:http://blog.andreloker.de/post/2008/06/Keep-your-config-clean-with-external-config-files.aspx
Basically, you can use "configSource" attribute to define which config file to refer. Note that the configuration file should be in same directory. if not, Refer here to solve the problem. .NET Config Files configSource outside the application directory folder

C# dotnet web application: Dll's xml config file is lost when Publishing

I'm a bit lost!
I have a dll that uses an xml config file for some db connection info. The dll looks for the xml config file in it's own directory and I can't change the dll at all.
Every time I build the project, I must manually copy the config file into a folder way down somewhere in the Temporary ASP.NET Files folder. (I don't understand this but I can live with this manual change)
The problem is that when I publish the project, I can't figure out where to copy the config file to?
Could someone please point me in the right direction? Or maybe show me a way that I can 'bind' the xml config file to the bin folder???
Vauneen
The .NET config file can be confusing to manage. The way it works in a webapp is that the Web.config will supersede any dependencies' app.config files (which is what I assume you're talking about when you say "DLL".)
Basically, in .NET all config info is pulled from the main app project.
See:
App.config seems to be ignored
and
Configuration from App.config isn't being pulled correctly
and finally:
Does a web.config substitute app.config?
will probably help you figure out what you need to know.
Update: Doing some further searching on your problem, it's possible that the code you're incorporating into the .Net Solution is using the "obsolete" ConfigurationSettings which will require you to add a reference to System.Configuration in your references folder (right-click on the project -> "Add References" and go to the .NET tab and select System.Configuration).
Set "Copy to Output Directory" to "Copy always" in properties of the configuration. Then Visual Studio will copy the configuration file automatically after each build and it will be properly published as well.

Why won't the appSettings file attribute allow a path to the parent directory?

I'm trying to deploy a project with multiple executables, some of which use a common config file. I'd like to have this common config file in its own directory. The problem is that it keeps getting ignored. I have the tag:
<appSettings file="..\Common Configs\Common.config">
in some of the executable-specific config files. If I copy the Common.config file to the same directory as the specific config file, and remove the path from the tag, everything works. It even works if I do something like:
<appSettings file="..\App1\Common.config">
where App1 is the folder the executable lives in.
This page suggests that the ConfigSource doesn't allow paths to the parent, but the file attribute does.
To further complicate matters, I'm deploying to a file share, so I can't create hard links in the exe folders.
Does anyone know why this doesn't work? How else can I deploy this with a shared config file?
The folder structure has the following form:
Solution
Common Configs
Common.config
App1
App1.exe
App1.exe.config
App2
App2.exe
App2.exe.config
If all of the projects are in the same solution, you can add the configuration file to the Solution Items area. This file can then be shared with all of the exes within the solution.
You will need to select each project that needs the configuration file and choose to Add Existing Item. From there, choose the file, then Add as Link.
This will allow you to maintain the one configuration file and have it deployed side by side with each exe.
Then when you reference it via the appSettings section, you can just reference the file directly.

.NET ApplicationSettings - Where's it loading the settings from?

I have settings that were originally scoped as User settings. I notice that the main .config that lives in the same directory as the assembly is being ignored by .NET. If I change any setting it still always ends up with the default values at run-time. I was unable to find a copy of the config file in User AppData. I changed the settings' scope to Application and yet .NET is still ignoring the config file.
Where is the file it's trying to load instead? How can I get it to only use the .config file in the assembly's directory?
If you are using visual studio to run your application, visual studio regenerates the settings file every time you complete a recompile.
So if you change your settings within the application and rebuild & launch within visual studio, the settings file will revert to what values are stored in the visual studio project.
If you use Configuration manager to read settings from config file, it has to load data from appname.exe.config file. Try this code (you have to add reference to system.configuration assembly)
System.Configuration.ConfigurationManager.AppSettings["settings-name"];

Add settings from another project to app.config

I have written an application in C# with a settings file (which is used to create an app.config file at compile time). This application uses a C# DLL which also has a settings file.
I read the following from this post:
If you build a project that references your DLL, you would add the same .settings file to that project and those settings would appear in the app.config file for the app and the DLL would be able to read those values. IF those values aren't in the app.config, the dll will fall back on the defaults.
I observed the DLL storing default values as this indicates it should. I right clicked on my application's project and selected Add Existing Item. Then I found the settings file from my DLL's project and added it to the application's project. My hope was that both the DLL settings file and the application settings file would be included in the application's app.config file. This way, the application's app.config file would override the defaults stored in the DLL. Unfortunately, this isn't happening.
So, my question is after adding the settings from the DLL project to the application project, how do I make the application project recognize the file and add its settings to the app.config file at compile time?
I'm unsure what you mean. Have you tried including it in a similar way to the following?
<appSettings file="dataSettings.config"/>

Categories