Avatar of Jeanette Durham
Jeanette Durham
Flag for United States of America asked on

vb.net 2008, Can you share a settings file amongst different projects in the same solution?

Dear Experts:

I was just wondering if vb.net 2008 could share settings? I have a service and a kinda over project all in the same solution. The over project just lets the user change settings and make decisions while the service will run without user interaction. I was thinking I could have it write out the settings in a file and let the service read it, but it'd be easier if there was a way to just access a certain project's settings from one program to the other (so my service knows where to put things) without making it so difficult. OR even a way to simply read the settings file in that's already there.

Thanks! ~Michael
Visual Basic.NET

Avatar of undefined
Last Comment

8/22/2022 - Mon
Nasir Razzaq


View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Jeanette Durham

It is true that a .ini file would work. I might end up doing that, but I kinda like the idea of a commen XML file as well, if only because I've never done one before and I love trying to learn and do new things. So I'm going to try to get some working code for the XML thing going and if it works better I'll go with that, otherwise with the INI file, which I know will work. I've used those before.

Is the right approach here to try to write out my own file and have the other app read it or try to work with one that is already there? It seems like from what I've read so far on this that the AppSettingsReader is the key to the problem?

Thanks! ~Michael

Unfortunately, as far as I could find out in the link I posted, all of the System.Configuration classes do not provide a means to change the settings file they work with.
There are some powerful functions there, but you can only use them for the configuration file that belongs to that particular application - that is web.config for a web page, and app.config, later renamed to executablename.config for a windows app.
Hence it makes it hard to use their functionality when you want to read settings from another application.

On the brighter side, XML provides plenty of classes that make it easy to read and write from and to XML files, hence the answer to your question is yes, the right approach is to write your own file and have the other app read it.

Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Jeanette Durham


Ok this is what I got so far. Seems to be a working system using XML. Are there any improvements to it you can see, easier better functions to call or anything, or is this the best way to do it?

Thanks! ~Michael

    ' Write Shared Settings to XML so service can read it
    Public Sub WriteSharedSettings()
        With My.Settings
            Using myWriter As New Xml.XmlTextWriter(GetApplicationDir() & "mySettings.xml", System.Text.Encoding.ASCII)
                myWriter.WriteElementString("prefsAutoDownload", .prefsAutoDownload)
                myWriter.WriteElementString("prefsAutoDelete", .prefsAutoDelete)
                myWriter.WriteElementString("prefsAutoMove", .prefsAutoMove)
                myWriter.WriteElementString("prefsNotificationOn", .prefsNotificationOn)
                myWriter.WriteElementString("prefsFTPServer", .prefsFTPServer)
                myWriter.WriteElementString("prefsUserName", .prefsUserName)
                myWriter.WriteElementString("prefsPassword", .prefsPassword)
                myWriter.WriteElementString("prefsFinalLocation", .prefsFinalLocation)
                myWriter.WriteElementString("prefsZipDirectory", .prefsZipDirectory)
            End Using
        End With
    End Sub
End Class
Public Module MySharedSettings
    Public prefsAutoDownload As Boolean
    Public prefsAutoDelete As Boolean
    Public prefsAutoMove As Boolean
    Public prefsNotificationOn As Boolean
    Public prefsFTPServer$
    Public prefsUserName$
    Public prefsPassword$
    Public prefsFinalLocation$
    Public prefsZipDirectory$
    Public Sub ReadSharedSettings()
        Dim myElem$ = "", myValu$ = ""
        Using myReader As New XmlTextReader(GetApplicationDir() & "mySettings.xml")
            Do While myReader.Read = True
                Select Case myReader.NodeType
                    Case Xml.XmlNodeType.Element
                        myElem = myReader.Name
                    Case Xml.XmlNodeType.Text
                        myValu = myReader.Value
                End Select
                If myElem <> "" And myValu <> "" Then
                    SaveSetting(myElem, myValu)
                    myElem = "" : myValu = ""
                End If
        End Using
    End Sub
    Private Sub SaveSetting(ByVal myElem$, ByVal myValu$)
        Select Case myElem
            Case "prefsAutoDownload" : prefsAutoDownload = myValu
            Case "prefsAutoDelete" : prefsAutoDelete = myValu
            Case "prefsAutoMove" : prefsAutoMove = myValu
            Case "prefsNotificationOn" : prefsNotificationOn = myValu
            Case "prefsFTPServer" : prefsFTPServer = myValu
            Case "prefsUserName" : prefsUserName = myValu
            Case "prefsPassword" : prefsPassword = myValu
            Case "prefsFinalLocation" : prefsFinalLocation = myValu
            Case "prefsZipDirectory" : prefsZipDirectory = myValu
        End Select
    End Sub

Open in new window


That looks good.
I would have chosen an XmlDocument, as it allows quick retrieval of data using SelectSingleNode and XPath