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.Configuration Settings.AppSettings.Get("
someKey"))
"Eric Sabine" <mopar41@____h_o_t_m_a_i_l_._ScPoAmM> wrote in message
news:OF**************@TK2MSFTNGP12.phx.gbl...
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(St ring 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