use single user.config for multiple .exe projects

M

Matt F

Two of the projects in my solution that both need to use a common
user.config file. This is a data application - the executable that is
created with the first project is the primary executable that users will be
working with 99% of the time, the other is a seperate "maintenance"
executable. The connection string is stored via My.Settings (encrypted of
course). The problem is that obviously with 2 different .exe there are 2
seperate user.config files, but I need to read the connection string out of
a single file. Is this even possible? If not, are there any other
suggestions for how to acheive this same type of functionality through other
methods?
 
L

Linda Liu [MSFT]

Hi Matt,

Firstly, in .NET 2.0, there are two kinds of application settings, i.e.
application-scoped settings and user-scoped settings. After the project is
compiled, the values of the application-scoped settings and the default
values of the user-scoped settings are stored in the application's config
file, named applicationname.exe.config in the same path of the exe file.

When the application is run, and the user changes the value of the
user-scoped settings and save them back to the disk, the new value of the
user-scoped settings are saved in another config file named user.config
located at the current user's application data path.

You have mentioned that you'd like to access the connection string stored
in one config file in two different applications. A connection string
setting should be a application-scoped setting and stored in
applicationname.exe.config file.

Yes, it's possible to do what you want, because actually config file is XML
formatted. We could regard the config file as a normal XML file.

The following is a sample.

Imports System.Xml

Private Function GetValueFromConfig(ByVal filepath As String, ByVal
connstrname As String) As String
Dim connstr As String = ""
Dim doc As New XmlDocument
doc.Load(filepath)

Dim connode As XmlNode = doc.SelectSingleNode("//connectionStrings")
Dim connChildnodes As XmlNodeList = connode.ChildNodes
Dim connChildnode As XmlNode
Dim nameAttr, connAttr As XmlAttribute

For Each connChildnode In connChildnodes
nameAttr = connChildnode.Attributes("name")
If (nameAttr.InnerText = connstrname) Then
connAttr = connChildnode.Attributes("connectionString")
Exit For
End If
Next
If Not (connAttr Is Nothing) Then
connstr = connAttr.InnerText
End If

Return connstr
End Function

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
dim connstr as string = Me.GetValueFromConfig("config1.config", "name
of the connection string setting")
End Sub

Hope this helps.
If you have any question, please feel free to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 

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

Top