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

x
?
Solved

Sharing Config File Setting

Posted on 2009-05-17
3
Medium Priority
?
268 Views
Last Modified: 2012-05-07
Hi Experts,
I've few application which share same settings. They all located at different folders. Some of the application are running as com dll so App.exe.config refers to the diff app name at run time.

Is there a way to use only 1 config file and shared by all the applications and com dlls? This will also simplify the process of maintaining too many config files with same settings.

Regards
PS
0
Comment
Question by:pcssecure
[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
  • 2
3 Comments
 
LVL 13

Expert Comment

by:Corey2
ID: 24406155
For more flexibility with settings I use xml serialization and a custom settings class.  You can then serialize the class to disk in a controlled location.

I normally get the application data folder and append the appropriate path which can be done similar to the SettingsLocation function below.   You can change AllUsersApplicationData to CurrentUserApplicationData for user specific settings.

Word to the wise use the ApplicationData location if you want your application to act nice with Vista.
Public Class MySettings
 
 
Private _Value1 as string
 
Property Value1 as string
get
 return _value1
end get
set (byval value as string)
 _value1 = value
end set
end property
 
 
Public Sub SerializeToXML (byval path as string)
'there is a nice snippet under XML for this
....
 
Public Shared Sub ReadXML (byval Path as string) as MySettings
'snippet
.....
 
Public Shared Function SettingsLocation () as string
        Dim fileLocation As String = My.Computer.FileSystem.SpecialDirectories.AllUsersApplicationData
        fileLocation = IO.Path.GetDirectoryName(fileLocation)
        fileLocation = IO.Path.GetDirectoryName(fileLocation)
        fileLocation = IO.Path.GetDirectoryName(fileLocation)
 
fileLocation = io.path.combine(filelocation,"MyAppDirectory\MySettingsFileName.xml"
 
end sub
 
 
 
end class

Open in new window

0
 

Author Comment

by:pcssecure
ID: 24406478
Hi Corey,
Thanks for the response.

To be more precise, I only need to read the settings.

It would be more helpful if could give me some working sample code.

Regards
-PS
0
 
LVL 13

Accepted Solution

by:
Corey2 earned 1500 total points
ID: 24406998
Even if you only need to read the settings it is nice to have a default xml created this example creates the xml if it doesn't exist. All you would have to do is modify the MySettings class to have a property for each setting you want.  The properties can also be classes as long as all the types are serializable.  (windows controls are not serializable)  anything you don't need to be serialized you can add an attribute of <System.Xml.Serialization.XmlIgnore()>
Imports System.Configuration
Imports System.Xml.Serialization
Imports System.IO
 
Public Class Form1
 
 
    Private _Stgs As MySettings = Nothing
    Public Property Stgs() As MySettings
        Get
            Return _Stgs
        End Get
        Set(ByVal value As MySettings)
            _Stgs = value
        End Set
    End Property
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Dim cs As ConnectionStringSettings
        'Dim bldTestDB As New System.Data.Common.DbConnectionStringBuilder
        'bldTestDB.ConnectionString
        'cs.ConnectionString
        If My.Computer.FileSystem.FileExists(MySettings.SettingsLocation) Then
            Stgs = MySettings.ReadXML(MySettings.SettingsLocation)
        Else
            Stgs = New MySettings
            Stgs.SerializeToXML(MySettings.SettingsLocation)
        End If
 
        Me.TextBox1.Text = Stgs.Value1
        Me.TextBox2.Text = String.Format("The Settings are saved to: {0}", MySettings.SettingsLocation)
    End Sub
End Class
 
Public Class MySettings
 
 
    Private _Value1 As String = "This is the Default Value"
 
    Property Value1() As String
        Get
            Return _value1
        End Get
        Set(ByVal value As String)
            _value1 = value
        End Set
    End Property
 
 
    Public Sub SerializeToXML(ByVal path As String)
        'there is a nice snippet under XML for this
        Dim writer As New XmlSerializer(GetType(MySettings))
        My.Computer.FileSystem.CreateDirectory(IO.Path.GetDirectoryName(path))
        Dim file As New StreamWriter(path)
        writer.Serialize(file, Me)
        file.Close()
        file.Dispose()
    End Sub
 
    Public Shared Function ReadXML(ByVal Path As String) As MySettings
        'snippet
        Dim reader = New XmlSerializer(GetType(MySettings))
        Dim file = New StreamReader(Path)
        Dim fileData = CType(reader.Deserialize(file), MySettings)
        file.Close()
        file.Dispose()
        Return fileData
    End Function
 
    Public Shared Function SettingsLocation() As String
 
        Dim fileLocation As String = My.Computer.FileSystem.SpecialDirectories.AllUsersApplicationData
        fileLocation = IO.Path.GetDirectoryName(fileLocation)
        fileLocation = IO.Path.GetDirectoryName(fileLocation)
        fileLocation = IO.Path.GetDirectoryName(fileLocation)
 
        fileLocation = IO.Path.Combine(fileLocation, "MyAppDirectory\MySettingsFileName.xml")
        Return fileLocation
 
    End Function

Open in new window

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

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…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

661 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