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

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.

Related

Using App.Config in a Windows Service in C# / Visual Studio

I have created a Windows Service that reads various paths from the app.config file and works correspondingly.
I also use the 'Visual Studio Installer' as a installer for my Service.
This is my project structure:
However, when I build the Installer, it does generate the .msi and .exe used to install my service, but it doesn't include the config file that it needs to use to fetch the paths it's using:
Normally, the only thing that I use is the .msi file to install and run the Windows Service (it works perfectly using hardcoded paths) but I can't seem to use it in combination with the config-file.
What I tried is to copy the config file from the service project, and paste it in the folder of the second screenshot, but the service doesn't work when I try to install and run it that way.
What am I doing wrong?
EDIT
I tried adding the .config file manually in the installer project:
Without success however
Nothing.
App.config is a part of the Service project and NOT of the installer.
It will be in the target installation folder of your Service assembly after installing.
Also App.config will be renamed according to the project's assembly name.
Example
Your assembly name is Watcher.Service, your .exe will be, after compiling Watcher.Service.exe and your app.config now will have the name Watcher.Service.exe.config.

service not reloading config file

I developed a windows service using Visual Studio 2012.
This service has a config file. When the application is build, the config file takes the name of the exe + .config.
If I install the service everything works, it means that the service uses the settings in the config file.
If I want to change a value in the config file and I restart the service, it does not read the new values, but it is still using the original values.
It looks like if the config file is copied in a different location when I install the service and the config file is not used anymore. Why? Where is the config file that service reads?
If you use .NET settings which you create in Visual Studio, then they are saved under
c:\Users\{Username}\AppData\Local\{AppName}\
this should work as you describe. you just have to be sure that you modify the correct .config file.
To check where your app resides, open the windows task-manager, right-click your exe and click 'properties'. here you can see the folder you app is running

.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"/>

c# winapp add app.config to installer?

Okay I've got my app.config file that is containing my database settings.
All works well on my development machine. But when I install it on a test machine I'm getting a null reference on the following line:
ConnectionString = ConfigurationManager.ConnectionStrings["MyDBConn"].ToString();
Why is this happening? I guess that the app.config file isn't found. But isn't this included when you build the setup?
I'm using a very simple setup project in VS2008.
The file app.Config is your source, don't distribute it. When Visual Studio builds your project it copies the file to {AppName}.exe.config (in the same folder as {AppName}.exe ) and that is the file you need to include in your setup.
Select app.config in solution explorer and in the properties tab choose the copy action:
Copy to Output Directory -> Copy always
or
Copy to Output Directory -> Copy if newer
Remember to rename the app.config to the name of the exe.
ie.
myprogram.exe would have an app.config called myprogram.exe.config

Categories