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.confi g". 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.Reflecti on
' at startup in *RELEASE* mode
' get the location of this executable
Dim appLocation As String = [Assembly].GetExecutingAs sembly.Location
File.Copy("mast er-location\app.co nfig", appLocation & ".config", True)
' read the config file (proof that it works)
'
Debug.WriteLine (System.Configu ration.Configur ationSettings.A ppSettings.Get( "
someKey"))
"Eric Sabine" <mopar41@____h_ o_t_m_a_i_l_._S cPoAmM> wrote in message
news:OF******** ******@TK2MSFTN GP12.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.InvalidO perationExcepti on: The key 'ConnectionStri ng' does not
exist in the appSettings configuration section.
at System.Configur ation.AppSettin gsReader.GetVal ue(String key, Type type)
at test_appconfig. Form1.FromAppli cationConfig(St ring 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 FromApplication Config(ByVal Key As String) As String
Try
Dim reader As New AppSettingsRead er
Return CStr(reader.Get Value(Key, GetType(String) ))
Catch ex As Exception
MessageBox.Show (ex.Message.ToS tring & vbCrLf & _
ex.Source.ToStr ing & vbCrLf & _
ex.StackTrace.T oString, "Function: FromApplication Config Exception",
MessageBoxButto ns.OK, MessageBoxIcon. Exclamation)
End Try
End Function