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

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.

Related

How to set the current\working directory of a WPF application to the installation path

In my C# WPF application I need to access some configuration files via a 3rd party library. This library requires to have the configuration file located in the same folder as the executable of my application. So i have no chance to change this behaviour.
While running my application in Visual Studio 2013 it works fine. I can access the configuration file since I have just copied it to the relevant folder.
But if I install my application an run it it cant locate my configuration files because it tries to find it under: Windows\system32.
No my approach is to make it happen that my application looks for the configuration file in the applications install folder.
How can I do that? How can I set the current\working directory of my application to a specific (installation) path in Visual Studio 2013?
try this
string path = AppDomain.CurrentDomain.BaseDirectory.ToString();
This will give path like "c:\\program...\\installdir\\" where your .exe is located.

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

installing multiple exe's at same time in window from application

I have two different windows application projects. I want that at the time of installation both will install together in same folder like one exe is installed.After installation both will work separately.There is an xml file which will be update by one project which have forms and another exe will use it which does not contain any form,just show the notify icon.Is it possible to create a setup file which will install both the exe's together? how do I give the path of the xml file?
You can use SetupProject to do it
Add setup project:
http://msdn.microsoft.com/en-us/library/19x10e5c(v=vs.80).aspx
Add primary outputs of both applications to it
http://msdn.microsoft.com/en-us/library/z11b431t(v=vs.80).aspx
If you want to place your xml file in your installation folder, there are permission issues to resolve if you want to change it later when your applications are running (usually admin privilages are needed), so you can't just add it to your setup project (that will only copy it to your installation path).
But after adding it to your setup project, you can copy your xml file to the location of your choice. Path of the xml file can be set using environment paths, for example Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
You can do it using a separate little console project that contains Installer class, where you copy your file and generally do additional things that need to be done during installation :
http://msdn.microsoft.com/en-us/library/system.configuration.install.installer.aspx
How to pass TARGETDIR into CustomActionsData?
http://www.codeproject.com/Articles/5821/All-about-Installers-Customizing-Windows-and-Web-s
http://msdn.microsoft.com/en-us/library/2w2fhwzz(VS.80).aspx
You can use TARGETDIR to get your installation path in yoour installerr class (just google it or use the link above, it's explained well there)
Then add your installer project's primary output to CustomActions in your SetupProject.

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.

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