How can i make a configuration in INI and XML so that i won't hardcode the location of my database and other configurations, and i would just change it from a form within my program? Can anyone help me? thanks..

Dani AI

Generated

Short practical summary tied to the replies from , and :

Visual Studio settings and a small settings-editor form work fine, but there are three important runtime/installer gotchas to understand before you choose INI/XML/app.config:

  • Settings scope: an Application-scoped setting is read-only at runtime; only User-scoped settings can be changed and persisted by code (My.Settings.Save writes per-user settings into a user.config under the user profile). (learn.microsoft.com)

  • app.config / connectionStrings: you can programmatically read or edit config sections (ConfigurationManager / OpenExeConfiguration) or use an external config file via configSource, but modifying deployed files depends on file permissions and the file location. If your app lives under Program Files the OS will normally prevent writes unless elevated. Consider file-location and installer behavior up front. (learn.microsoft.com)

  • Secrets: avoid storing DB passwords in plain text. If you must persist them, use protected configuration (DPAPI or RSA providers) or an OS-secure store. (learn.microsoft.com)

Recommended patterns (short, practical):

  • Per-user editable settings: store editable parts (server, port, db name, maybe username) in a user-scoped settings or a small XML file under %APPDATA% and build the connection string at runtime. This avoids elevation and makes per-user switching trivial. For MySQL the usual keys are Server/Uid/Pwd/Database/Port; follow the connector docs when building the string. (mysqlconnector.net)

  • Per-machine editable settings (admin-controlled): put a template in ProgramData (or use an external connections.config referenced from app.config) and have your installer place it there, with appropriate ACLs. Your app reads that file at startup; if the app must update it, ensure the installer or updater sets write permissions or run the update elevated. (learn.microsoft.com)

Small VB.NET example: save a simple XML config into AppData (System.Xml.Linq).

' Requires: Imports System.IO, Imports System.Xml.Linq
Dim folder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),"MyCompany","MyApp")
Directory.CreateDirectory(folder)
Dim cfg = Path.Combine(folder,"settings.xml")
Dim doc = New XDocument(
  New XElement("Settings",
    New XElement("Server", txtServer.Text),
    New XElement("Port", txtPort.Text),
    New XElement("Database", txtDatabase.Text),
    New XElement("User", txtUser.Text)
  )
)
doc.Save(cfg)

What to watch for: decide per-user vs per-machine, never assume app.config is writable after install, and do not store cleartext passwords without encryption. The links above explain the runtime behavior and safe options in detail.

Recommended Answers

All 8 Replies

Usually VS saves connectionstrings in .config file or .settings file which is xml formatted. to do it yourself vs main menu Project->Add new item->.settings file
Add your items in this settings file, you're now can modify your settings without modifying your code.
Avoid using ini files, xml is better.

Thanks! is there a way that i can do it using my program in vb? like having a form where i can edit all the settings for my program in xml?

Sure, you've ml file and you also have System.Xml namespace which contains a lot of classes to add\remove and edit some nodes in xml files...

I'm kind lost there.. Can you teach me how? a little sample code will do, if you don't mind..

I'm kind lost there.. Can you teach me how? a little sample code will do, if you don't mind..

Create a new dialog form and use it to manipulate configuration items. On form_load add the items from your program settings:

Private Sub frmConfig_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        '
        '   Set the default values into the controls via the program settings
        '
        bLoading = True
        Me.txtServer.Text = My.Settings.Server
        Me.txtDB.Text = My.Settings.Database       
        bLoading = False
    End Sub

Set flags if they change any of the values and then on the OK_Button_Click test for changes and save them if they exist:

Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        '
        '   See if we have anything to save
        '
        If Me.bServerChanged Then
            My.Settings.Server = Me.txtServer.Text
        End If
        If Me.bDBChanged Then
            My.Settings.Database = Me.txtDB.Text
        End If
        '
        '   If anything changed, save the changes to the setting file
        '
        If Me.bServerChanged Or Me.bDBChanged Then
            My.Settings.Save()
        End If
        Me.DialogResult = System.Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

Please, tell me what exactly what do you want to do ??

I want to have a configuration settings editor for my program with mysql.. I want the users to be able to switch from one server to the other using a form. If ill be making an installer for my project where would the config go? can it be read by the application still? or will i specify a path where the application will read its settings..

From VS main menu Project->Add new item select .settings file, once you've added it a designer (tabular) form allows you to add variables add ServerName and assign it a default value.
In coding let's the connection string static except the server name dynamic from the settings file.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.