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

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"];

Related

Adding app.config (Application Configuration File) in Visual Studio Professional 2017

I can't get the Application Configuration File template to show up under my Add > New Item > Visual C#. I've run a repair. I've uninstalled and reinstalled. I've deleted and added Cache folders. I've run devenv.exe /InstallVSTemplates. Nothing is working.
If anyone can tell me specifically what installation elements I should be including, and specifically what type of Project I need to start that might help.
I want to create a simple .Net Web Form in .Net and C# and need to include an app.config file.
When you create a new project (depending on the type of project like a web application for example) it will automatically create a template app.config file.
Worst case just make a txt file in notepad and call it [Appname].config
Another way would be to go in the project properties, then the Settings tab. If there are no settings yet, a link should exist in the middle of the empty tab to create a default setting file. Add one Application or User setting and save. Among other things a config file will be created for you too.

How do I not regenerate the .manifest file for my C# VS2013 project?

I have a C# solution in Visual Studio 2013, and every time I run it, it generates a Brilliance.exe.manifest file. I would like this file to not be generated every time I build and debug my application. My application's name is "Brilliance"
In my project settings I tried setting the "Icon and manifest" option for Manifest to be both Embed manifest with default settings and Create application without a manifest and neither seem to have any effect.
There is no file by this name in the solution, and I don't know why it is being created.
Edit: Updated full manifest name

C# - app config doesn't change

I want to save some settings on a config file for future use.
I'm trying to use the regular code that I see on all the tutorials -
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings["username"].Value = m_strUserName;
// I also tried -
//config.AppSettings.Settings.Remove("username");
//config.AppSettings.Settings.Add("username", m_strUserName);
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
Now - I can see that on runtime - the file "...vshost.exe.config" on "Debug" folder is changes, nut when I close my application - all the changes are deleted.
What can I do?
To test using the normal exe's config file un-check the box for "Enable the Visual Studio Hosting Process" in the "Debug" tab in the project properties menu. That will make visual studio no-longer use the vshost.exe file to launch and the correct config file will be used.
When you deploy your application to your end users, there is no vshost.config.
Your changes will be applied to the real exe.config. So you don't have to worry for this.
When you build your application in a debug session, the app.config file, present in your project, gets copied over to the output directory. Then this config file gets copied to vshost.config as well. In this way the contents of app.config overwrites any changes made during a debug session in the vshost.exe.config.
However let me say that writing this kind of information into an application config is a bad practice. The configuration file should be used only to store permanent configuration that usually don't change during the lifetime of your application. Connection settings, for example, are valid info to store there because you normally don't change these and you don't want to hard code them.
Settings like the user name should use user.config instead. This config is per-user/per-app and allows read/write access.

Why doesn't app.config get added after installing from windows installer?

I have a Windows application developed using Visual Studio 2008 (C#).
It has a app.config file, where various configuration related information are kept. After creating an installer for the project and installing it, there are no app.config file being copied to the installed directory. However, the functionalities that rely on settings in this file seem to work.
Now one of these settings is a database connection string, which obviously needs changing when installed in a different PC. My question is, how to have the app.config file available with the Setup file so that it can be configured later?
The app.config is copied/renamed <assembly name>.config as part of the compilation process, and placed in the bin directory. If you're using a Visual Studio Installer project (blech!), then it should have picked it up also and included it in the installer, IIRC.
(In response to your comment to both answers)
You can't keep using it as "app.config". The .NET config system searches for the configuration file whose name is the same as the entry assembly. If you renamed the file back to "app.config", then the configuration classes would stop working.
Your app.config file gets renamed on compilation, with the name of the binary. I.e. if your binary is myapp.exe then your app.config will be renamed to either myapp.config or myapp.exe.config.
This is the file that you should add to the setup package in order to use it on deployment for configuration.

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