Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

VB.NET Sharing config info between main and class library modules

Posted on 2008-10-16
11
Medium Priority
?
896 Views
Last Modified: 2008-10-20
I have a solution composed of a console project and 5 class libaries that the console app uses.  Unfortunately,  I have 6 app.config files and  need to be able to get back configuration information that is "shared".  So far I have not found the method to achieve this.

For experimentation,  I created another solution which is a windows app and a class library.  In each I have created a shared method that attempts to read the app.config setting.  Both routines are identical and I had hoped that the exe and dll would both return their respective values, but such was not the case.  It sort of makes sense that the reason would be since the DLL "lives" within the EXE once it has been instantiated, that it would look at the EXE's app.config not it's own, but is not what I need.  I tried similar with my.settings... and again there is a settings page for either module (exe or dll).

I will include both modules and hope that someone has a clue about all this.

Thanks,
Howard

'--------------------------------------------------------------------------------------
'EXE Follows
 
Imports SomeOtherAppConfigLib
 
Public Class frmMain
    Dim sDLLConfigString As String = SomeOtherAppConfigLib.Constants.SomeConnectionString
    Dim sMainAppConfigString As String = Constants.SomeConnectionString
    Dim sSettingsConfigString As String = SomeOtherAppConfigLib.Constants.ShowMySettingsValue
 
    Sub ShowValues()
        MsgBox(sDLLConfigString)
        MsgBox(sMainAppConfigString)
    End Sub
 
    Private Sub frmMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ShowValues()
    End Sub
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        MsgBox(SomeOtherAppConfigLib.Constants.SomeConnectionString)
    End Sub
End Class
'--------------------------------------------------------------------------------------
'EXE's CONSTANT.VB Follows
 
Imports System.Configuration.ConfigurationManager
 
Public Class Constants
    Public Shared ReadOnly Property SomeConnectionString() As String
        Get
            Return AppSettings("conFoobar")
        End Get
    End Property
 
    Public Shared ReadOnly Property ShowMySettingsValue() As String
        Get
            Return My.Settings.GetValueLoc  '<--this setting made in the exe module just as it was in the DLL
        End Get
    End Property
 
End Class
'--------------------------------------------------------------------------------------
'--------------------------------------------------------------------------------------
'DLL Follows
Public Class SomeOtherAppConfigLib
End Class
'--------------------------------------------------------------------------------------
'DLL's CONSTANT.VB Follows
 
Imports System.Configuration.ConfigurationManager
 
Public Class Constants
    Public Shared ReadOnly Property SomeConnectionString() As String
        Get
            Return AppSettings("conFoobar")
        End Get
    End Property
 
    Public Shared ReadOnly Property ShowMySettingsValue() As String
        Get
            Return My.Settings.GetValueLoc
        End Get
    End Property
 
End Class

Open in new window

0
Comment
Question by:Howard Bash
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
11 Comments
 
LVL 13

Expert Comment

by:TechTiger007
ID: 22732908
You can load a different configuration file with the following method
You will have to include System.Configuration assembly to your application reference

dim config as System.Configuration.Configuration = System.Configuration.ConfigurationManager.OpenExeConfiguration(<configuration path>);
0
 
LVL 1

Author Comment

by:Howard Bash
ID: 22734197
If have pasted my attempt... It may work if I can get the line with the <--- ??? or a variation to work.

Please advise.

Imports System.Configuration.ConfigurationManager

Public Class Constants
    Private Shared config As System.Configuration.Configuration

    Public Shared ReadOnly Property SomeConnectionString() As String
        Get
            Dim sPath As String = "C:\mycode\PlayingWithAppConfig\PlayingWithAppConfig\app.config"
            config = System.Configuration.ConfigurationManager.OpenExeConfiguration(sPath) '<---???
            Return config.AppSettings("conFoobar")
        End Get
    End Property

End Class
0
 
LVL 13

Expert Comment

by:TechTiger007
ID: 22734766
Here it goes. I tested this in C# and converted to VB.NET, May contain some syntax issues

            dim map as new System.Configuration.ExeConfigurationFileMap()
            map.ExeConfigFilename = "app.config"
            dim config as new       System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(map,System.Configuration.ConfigurationUserLevel.None)
            dim value as string = config.AppSettings.Settings["connectionstring"].Value


0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 1

Author Comment

by:Howard Bash
ID: 22734994
OpenMappedExeConfiguration is not defined in VS2008 with System.Configuration in my references.

Otherwise the only syntax issue is that VB uses "()"  whereas C/C++/C# use "[]".


0
 
LVL 13

Expert Comment

by:TechTiger007
ID: 22735109
OpenMappedExeConfiguration is defined in dll System.Configuration, it is not added to your reference by default you will have to add it. The namespace "configuration" is defined in multiple dlls, I was also wondering about this and when I added System.Configuration in my reference it worked fine

0
 
LVL 1

Author Comment

by:Howard Bash
ID: 22735648
I added the reference and also the Imports System.Configuration which is like the Uses statement that I imagine you must be using also.
0
 
LVL 13

Expert Comment

by:TechTiger007
ID: 22738022
OpenMappedExeConfiguration is introduced from dotnet 2.0 and it is part of 3.0 and 3.5
It is in system.configuration.dll file and System.configuration namespace

http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.openmappedexeconfiguration.aspx

It should be appearing if you have added system.configuration dll as reference in your project. hmmm....
Ok try to close and reopen the solution, also try to remove and add the reference. May be visual studio did not update the references properly.
0
 
LVL 1

Author Comment

by:Howard Bash
ID: 22743216
Removing and adding to not correct this.  Also the version of system.configuration.dll is
v2.0.50727.
0
 
LVL 13

Accepted Solution

by:
TechTiger007 earned 2000 total points
ID: 22747492
I am terribly sorry  Not sure how this changed while copy pasting

Please use

Dim config As Configuration = ConfigurationManager.OpenMappedExeConfiguration(map, System.Configuration.ConfigurationUserLevel.None)

instead of
dim config as new       System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(map,System.Configuration.ConfigurationUserLevel.None)
0
 
LVL 1

Author Comment

by:Howard Bash
ID: 22761919
Worked like a charm!!!
0
 
LVL 13

Expert Comment

by:TechTiger007
ID: 22762067
Good to hear that
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question