Shared app.config

E

Eric Sabine

This situation requres many exes to be sitting on a network share. I would
like all of these to use the same app.config file, which will be stored in
the same location on the network. A quick test though gave me this
exception:

System.InvalidOperationException: The key 'ConnectionString' does not exist
in the appSettings configuration section.
at System.Configuration.AppSettingsReader.GetValue(String key, Type type)
at test_appconfig.Form1.FromApplicationConfig(String Key) in
F:\VB.NET\junk\test_appconfig\test_appconfig\Form1.vb:line 62
The program '[2808] test_appconfig.exe' has exited with code 0 (0x0).

What's the best way to share the app.config in this common network folder?
Thanks, Eric


Public Function FromApplicationConfig(ByVal Key As String) As String
Try
Dim reader As New AppSettingsReader
Return CStr(reader.GetValue(Key, GetType(String)))
Catch ex As Exception
MessageBox.Show(ex.Message.ToString & vbCrLf & _
ex.Source.ToString & vbCrLf & _
ex.StackTrace.ToString, "Function: FromApplicationConfig Exception",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End Try
End Function
 
C

CJ Taylor

Assign them the same appDomain. I "believe" thats what is used to read the
app.config file... I could be wrong on that. But tests that I've done show
it.

-CJ
 
E

Eric Sabine

Thanks CJ, so then I would be creating a new appdomain in my sub main? In
what way though do I instruct each exe to share this appdomain? Are the
parameters for itself somehow loaded from some additional universal file.
This is new territory for me.

Could you share and representative code please?

Eric


CJ Taylor said:
Assign them the same appDomain. I "believe" thats what is used to read the
app.config file... I could be wrong on that. But tests that I've done show
it.

-CJ

Eric Sabine said:
This situation requres many exes to be sitting on a network share. I would
like all of these to use the same app.config file, which will be stored in
the same location on the network. A quick test though gave me this
exception:

System.InvalidOperationException: The key 'ConnectionString' does not exist
in the appSettings configuration section.
at System.Configuration.AppSettingsReader.GetValue(String key, Type type)
at test_appconfig.Form1.FromApplicationConfig(String Key) in
F:\VB.NET\junk\test_appconfig\test_appconfig\Form1.vb:line 62
The program '[2808] test_appconfig.exe' has exited with code 0 (0x0).

What's the best way to share the app.config in this common network folder?
Thanks, Eric


Public Function FromApplicationConfig(ByVal Key As String) As String
Try
Dim reader As New AppSettingsReader
Return CStr(reader.GetValue(Key, GetType(String)))
Catch ex As Exception
MessageBox.Show(ex.Message.ToString & vbCrLf & _
ex.Source.ToString & vbCrLf & _
ex.StackTrace.ToString, "Function: FromApplicationConfig Exception",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End Try
End Function
 
C

CJ Taylor

Your sub main creates an appdomain nonetheless on a current thread with the
same name as the executable.

So it looks like you can create a new Domain... and run
AppDomain.ExecuteAssembly() and get the desired results. I have never
actually done this, I'm just looking at things and think it can do it..

But hey, not a lot of code to test out..

Lookup AppDomain.ExecuteAssembly in your help file it gives an example.

-CJ

Eric Sabine said:
Thanks CJ, so then I would be creating a new appdomain in my sub main? In
what way though do I instruct each exe to share this appdomain? Are the
parameters for itself somehow loaded from some additional universal file.
This is new territory for me.

Could you share and representative code please?

Eric


CJ Taylor said:
Assign them the same appDomain. I "believe" thats what is used to read the
app.config file... I could be wrong on that. But tests that I've done show
it.

-CJ
stored
in
the same location on the network. A quick test though gave me this
exception:

System.InvalidOperationException: The key 'ConnectionString' does not exist
in the appSettings configuration section.
at System.Configuration.AppSettingsReader.GetValue(String key, Type type)
at test_appconfig.Form1.FromApplicationConfig(String Key) in
F:\VB.NET\junk\test_appconfig\test_appconfig\Form1.vb:line 62
The program '[2808] test_appconfig.exe' has exited with code 0 (0x0).

What's the best way to share the app.config in this common network folder?
Thanks, Eric


Public Function FromApplicationConfig(ByVal Key As String) As String
Try
Dim reader As New AppSettingsReader
Return CStr(reader.GetValue(Key, GetType(String)))
Catch ex As Exception
MessageBox.Show(ex.Message.ToString & vbCrLf & _
ex.Source.ToString & vbCrLf & _
ex.StackTrace.ToString, "Function: FromApplicationConfig Exception",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End Try
End Function
 
R

Russell Jones

The application configuration file must reside in the application directory
and must be named the same as the exe file, with a .config extension. For
example, if your application exe is "test.exe" the config file will be
"test.exe.config". The simplest way to solve your problem is to dynamically
copy the app.config master copy to the application directory for each
application, renaming it appropriately when the application starts. For
example, this code will work. Note that it only works as written in
*release* mode, not in debug mode. However, you can probably add an ifdef
and change the pathname for the copied file to make it work in either--I
didn't test that.

Also note that, as written, there's a hard-coded path to the master
app.config file in the code, which is generally a bad idea. You'd be better
off retreiving the master pathname from the Registry, from some text file,
passing it in as a startup parameter, or getting it from a database

Put this code in your startup form load or in the module containing Sub
Main, changing the master-location path and "someKey" appropriately.
Imports System.IO
Imports System.Reflection
' at startup in *RELEASE* mode
' get the location of this executable
Dim appLocation As String = [Assembly].GetExecutingAssembly.Location
File.Copy("master-location\app.config", appLocation & ".config", True)
' read the config file (proof that it works)
'
Debug.WriteLine(System.Configuration.ConfigurationSettings.AppSettings.Get("
someKey"))
 
S

Stephen Muecke

Eric,

You can put the settings in the machine.config file.
Its located in the CONFIG directory of the runtime install path
(System.Web.Hosting.SimpleWorkerRequest.MachineConfigPath Property will
return the path)
machine.config already contains an <appSettings> although it is commented
out
AppSettingsReader first checks this file before checking the applications
configuration file

Stephen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top