AppSettingsReader

  • Thread starter Thread starter Erick
  • Start date Start date
E

Erick

Hi,

Could any explain how AppSetting know the right app.config file to take as
default?

Thanks.
 
Hi Erick,

For windows forms the AppSetting uses the config file called <executable
name>.config, if your excutable is called test.exe the config file is
called test.exe.config. This file must reside in the same folder as your
executable.
If you add an app.config to your solution, Visual Studio will automatically
copy and rename this file to Debug/Release folder.
The way AppSetting gets hold of this file should not consern us, but here is
a possible way it is implemented:

System.Reflection.Assembly asm =
System.Reflection.Assembly.GetExecutingAssembly();
string configname = asm.Location + ".config";

In WebForms the config file is called Web.config, but this is not renamed,
it is always called web.config.

Config files and dll's.
If you use AppSetting in code that is run from a DLL, it will look in the
config file for the executable that called code in the DLL. DLL's don't have
config files, with the exception of WebForms. If the code in your DLL uses
any of the Visual Studio designers and links a property to a value in the
config file (connection string) , the DLL project will get an app.config
file. This file will only be used by the designers, the values must be
copied to the config file of the executable for the runtime code to work.

Hope this helps

Chris
 
Because your config file must be name exactly as your application with
..config at the end (aap.exe.config) and also sited into the same folder as
your .exe.

--


HTH

Éric Moreau, MCT, MCSD
Conseiller principal / Senior consultant
Concept S2i inc. (www.s2i.com)
 
Thanks, Eric

Your comments are very usefull, base on you answare I have additional doubt
in wy an Installer class cannot read the .config file of a window service
I'm writing
I want to use dinamic property asignment for the ServiceInstaller class
intance, like
the ServiceName & DisplayName. The code look like this and is into the
InitializeComponents method:
+------------------------------------------------------------------+

System.Configuration.AppSettingsReader configurationAppSettings = new
System.Configuration.AppSettingsReader();

this.m_MonitorInstaller.DisplayName =
((string)(configurationAppSettings.GetValue("key_DisplayName",
typeof(string))));
this.m_MonitorInstaller.ServiceName =
((string)(configurationAppSettings.GetValue("Key_ServiceName",
typeof(string))));

+------------------------------------------------------------------+

At the time to run the InstallUtil to install the service I got this errors:
+------------------------------------------------------------------+
Unable to create an instance of the
GroupEx.FaxMonitorService.Installer.FaxMonit
orInstaller installer type.
System.Reflection.TargetInvocationException: Exception has been thrown by
the ta
rget of an invocation.
The inner exception System.InvalidOperationException was thrown with the
followi
ng error message: The key 'key_DisplayName' does not exist in the
appSettings co
nfiguration section..

An exception occurred during the Install phase.
System.InvalidOperationException: Unable to create an instance of the
GroupEx.Fa
xMonitorService.Installer.FaxMonitorInstaller installer type.
at
System.Configuration.Install.AssemblyInstaller.InitializeFromAssembly()
at System.Configuration.Install.AssemblyInstaller.Install(IDictionary
savedSt
ate)
at System.Configuration.Install.Installer.Install(IDictionary stateSaver)
at System.Configuration.Install.TransactedInstaller.Install(IDictionary
saved
State)
The inner exception System.Reflection.TargetInvocationException was thrown
with
the following error message: Exception has been thrown by the target of an
invoc
ation..
at System.RuntimeType.CreateInstanceImpl(Boolean publicOnly)
at System.Activator.CreateInstance(Type type, Boolean nonPublic)
at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder
bin
der, Object[] args, CultureInfo culture, Object[] activationAttributes)
at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr,
Binde
r binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
at
System.Configuration.Install.AssemblyInstaller.InitializeFromAssembly()
The inner exception System.InvalidOperationException was thrown with the
followi
ng error message: The key 'key_DisplayName' does not exist in the
appSettings co
nfiguration section..
at System.Configuration.AppSettingsReader.GetValue(String key, Type type)
at
GroupEx.FaxMonitorService.Installer.FaxMonitorInstaller.InitializeComponen
t() in c:\documents and settings\epimlea\my documents\visual studio
projects\gro
upexfaxmonitorservice\faxmonitorinstaller.cs:line 58
at GroupEx.FaxMonitorService.Installer.FaxMonitorInstaller..ctor() in
c:\docu
ments and settings\epimlea\my documents\visual studio
projects\groupexfaxmonitor
service\faxmonitorinstaller.cs:line 33

The Rollback phase of the installation is beginning.
+------------------------------------------------------------------+
I wonder if the InstallUtil tool is looking for some other name of the
assembly?

Thanks for any additional help you can provide.

Regards...
 
Thanks, Chris

Your comments are very usefull, base on you answare I have additional doubt
in wy an Installer class cannot read the .config file of a window service
I'm writing
I want to use dinamic property asignment for the ServiceInstaller class
intance, like
the ServiceName & DisplayName. The code look like this and is into the
InitializeComponents method:
+------------------------------------------------------------------+

System.Configuration.AppSettingsReader configurationAppSettings = new
System.Configuration.AppSettingsReader();

this.m_MonitorInstaller.DisplayName =
((string)(configurationAppSettings.GetValue("key_DisplayName",
typeof(string))));
this.m_MonitorInstaller.ServiceName =
((string)(configurationAppSettings.GetValue("Key_ServiceName",
typeof(string))));

+------------------------------------------------------------------+

At the time to run the InstallUtil to install the service I got this errors:
+------------------------------------------------------------------+
Unable to create an instance of the
GroupEx.FaxMonitorService.Installer.FaxMonit
orInstaller installer type.
System.Reflection.TargetInvocationException: Exception has been thrown by
the ta
rget of an invocation.
The inner exception System.InvalidOperationException was thrown with the
followi
ng error message: The key 'key_DisplayName' does not exist in the
appSettings co
nfiguration section..

An exception occurred during the Install phase.
System.InvalidOperationException: Unable to create an instance of the
GroupEx.Fa
xMonitorService.Installer.FaxMonitorInstaller installer type.
at
System.Configuration.Install.AssemblyInstaller.InitializeFromAssembly()
at System.Configuration.Install.AssemblyInstaller.Install(IDictionary
savedSt
ate)
at System.Configuration.Install.Installer.Install(IDictionary stateSaver)
at System.Configuration.Install.TransactedInstaller.Install(IDictionary
saved
State)
The inner exception System.Reflection.TargetInvocationException was thrown
with
the following error message: Exception has been thrown by the target of an
invoc
ation..
at System.RuntimeType.CreateInstanceImpl(Boolean publicOnly)
at System.Activator.CreateInstance(Type type, Boolean nonPublic)
at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder
bin
der, Object[] args, CultureInfo culture, Object[] activationAttributes)
at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr,
Binde
r binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
at
System.Configuration.Install.AssemblyInstaller.InitializeFromAssembly()
The inner exception System.InvalidOperationException was thrown with the
followi
ng error message: The key 'key_DisplayName' does not exist in the
appSettings co
nfiguration section..
at System.Configuration.AppSettingsReader.GetValue(String key, Type type)
at
GroupEx.FaxMonitorService.Installer.FaxMonitorInstaller.InitializeComponen
t() in c:\documents and settings\epimlea\my documents\visual studio
projects\gro
upexfaxmonitorservice\faxmonitorinstaller.cs:line 58
at GroupEx.FaxMonitorService.Installer.FaxMonitorInstaller..ctor() in
c:\docu
ments and settings\epimlea\my documents\visual studio
projects\groupexfaxmonitor
service\faxmonitorinstaller.cs:line 33

The Rollback phase of the installation is beginning.
+------------------------------------------------------------------+
I wonder if the InstallUtil tool is looking for some other name of the
assembly?

Thanks for any additional help you can provide.

Regards...
 
Erick,
In addition to the others comments.

My understanding is that AppSettings uses the
AppDomainSetup.ConfigurationFile setting for the right app.config file to
use.

That the framework itself will initialize this AppDomainSetup setting to
exectubleName.exe.config when it creates the initial appdomain for programs
or web.config for ASP.NET.

What this means is that you can override the app.config to what ever you
want when you use an System.AppDomainSetup object when you create new
AppDomains (System.AppDomain). Allowing you to get a config file for a DLL
for example. Just remember the DLL then needs to run in its own app domain.

Hope this helps
Jay
 
Thanks a lot Jay

This info helps, the reason of this question about the AppSettingsReder is
that I'm trying to install a window service and into the Intall class i
setup some
properties dinamically by storing some key into the app.config file. But at
the time
to run the InstallUtil I got some errors throwed by the installer.

An exception occurred during the Install phase.
System.InvalidOperationException: Unable to create an instance of the
MonitorService.Installer.MonitorInstaller installer type.
The inner exception System.Reflection.TargetInvocationException was thrown
with the following error message: Exception has been thrown by the target of
an invocation..
The inner exception System.InvalidOperationException was thrown with the
followi
ng error message: The key 'key_DisplayName' does not exist in the
appSettings configuration section..

Please advice... Thanks..

Regards...
 
Erick,
Unfortunately the installer does not run your EXE, I understand that it
loads your exe, and dynamically creates & runs the installer classes. So it
is actually running as installutil.exe...

You may want to open the config itself directly in the installer class and
go from there.

Hope this helps
Jay
 
Jay said:
Erick,
Unfortunately the installer does not run your EXE, I understand that it
loads your exe, and dynamically creates & runs the installer classes. So it
is actually running as installutil.exe...

You may want to open the config itself directly in the installer class and
go from there.

Hope this helps
Jay

Search for "Walkthrough: Passing Data to a Custom Action" in MSDn. That
gives a VB example of a installer which reads a configuration file and
edits it.
 
Back
Top