How can I save the location (Path) of the MS Access database I want to connect to in my app without having a second database local to the VB Client. Obviously, the client needs to know the path to the database in order to connect, but where can this path be stored so as to not be hardcoded

Saving location of database in the app, not the database
yjoe3
hwg_Maarten
Danny Tykon
I did a little investigation about the config files. I think it would work, but how I understand a little about XML and I can sort of follow the examples in the online help, but like all topics, they are very limited.
I need to have one line stored with the physical location of the database that the client should connect to. Obviously, the first time the client is run after installation, there will be no value for this entry. After inputting one (on a startup form) the config file should be updated and then the client can access that updated path and save it to a public variable for the session. All ADO.Net connections will use this variable in the connection string.
I need to know how to read this path from the config file and write changes to it in the config file Can you help
Howard555
I am having some issues with these config files as well... I did what you said above (adding an app config file to the project) but when I run the code below (called from Form_Load) it loads the settings from app.config in the project folder rather than "app".exe.config in the Bin folder... how do I tell the app to use the proper config file
<Code>
Private Sub LoadSettings()
'Load the settings from the XML config file.
Dim colValues As System.Collections.Specialized.NameValueCollection
colValues = Configuration.ConfigurationSettings.AppSettings()
txtDefaultFolder.Text = colValues.Get("default_folder")
arrFileExtensions = Split(colValues.Get("extensions"), ",")
End Sub
I also did what Jacob suggested above to write out the settings to the config file and that works fine, but I can't seem to get the app to read it in from the same file... can you see what I'm doing wrong here
Cheers,
Mike.
ADPerkin
McCloud
To read:
System.Configuration.ConfigurationSettings.AppSettings("ConnectionString").ToString
To write:
m_DefaultConnectionString = "server=" & ServerValue & ";database=" & DatabaseValue & ";Trusted_Connection=true"
Try
Dim Asm As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly
Dim FileInfo As System.IO.FileInfo = New System.IO.FileInfo(Asm.Location + ".config")
'' Load the config file into the XML DOM.
Dim XmlDocument As New System.Xml.XmlDocument()
XmlDocument.Load(FileInfo.FullName)
Dim Node As System.Xml.XmlNode
Dim FoundIt As Boolean = False
' Find the right node and change it to the new value.
For Each Node In XmlDocument.Item("configuration").Item("appSettings")
If Node.Name = "add" Then ' skip any comments
If Node.Attributes.GetNamedItem("key").Value = "ConnectionString" Then
Node.Attributes.GetNamedItem("value").Value = "server=" & ServerValue & ";database=" & DatabaseValue & ";Trusted_Connection=true"
FoundIt = True
End If
End If
Next
If Not FoundIt Then
Dim a As Xml.XmlElement = XmlDocument.CreateElement("add")
Dim b As Xml.XmlAttribute = XmlDocument.CreateAttribute("key")
Dim c As Xml.XmlAttribute = XmlDocument.CreateAttribute("value")
a.Attributes.Append(b).Value = "ConnectionString"
a.Attributes.Append(c).Value = "server=" & ServerValue & ";database=" & DatabaseValue & ";Trusted_Connection=true"
XmlDocument.Item("configuration").Item("appSettings").AppendChild(a)
End If
' Write out the new config file.
XmlDocument.Save(FileInfo.FullName)
Catch ex As SystemException
ExceptionManager.Publish(ex)
MsgBox(ex.Message & "<-->" & ex.Source & " " & ex.TargetSite.Name)
End Try
At least, that's how I've done it in the past... Perhaps someone else can provide an alternative. Actually, you may want to check this out <a href="http://staff.develop.com/candera/weblog/stories/2003/02/20/theLastConfigurationSectionHandlerIllEverNeed.html">here</a>.
IMBack
Niran
What I ended up doing was not using a '.config' extension but a '.ini' one - that way I can still use the same code but my changes don't get wiped out when I compile.
Or I might abandon using that sort of config file and do my own from a straight XML file as I found at George Shepherd's (completely brilliant!) Windows Forms FAQ's <a href="http://www.syncfusion.com/faq/winforms/search/910.asp">here</a>. Then I can just call it "<app>".ini and simply include in my distribution package. (There's still a lot to be said for the KISS principle!!)
Would still like to get any feedback if anyone can see I'm doing something daft!
Mike.