Currently I've been working on a program and I needed to save settings and I didn't wanna use the registry because I don't quiet know the permissions on the machine this program will be running on. So I did some reading on XML and found it to be quiet simple to write out to an xml file.


Start a new class
I called mine settingsClass


Since we are using XML we need to use this import:
Code:
Imports System.Xml
Now there are a few ways to do this, I haven't gotten around to reading the XML file back in to load the settings so you could have writeonly properties but since I know I will be reading from them as well I left them the way they are. You could name the properties whatever you need, just for my project I called them IP and port.

Declare local Variables:
Code:
    Private _IPaddressString As String
    Private _PortNumberString As String
Write the Properties that will receive input and give back to the whatever calls it.

Code:
    Property setIPAddress() As String

        Get
            Return _IPaddressString
        End Get
        Set(ByVal value As String)
            _IPaddressString = value
            Call SaveSettings()
        End Set
    End Property

    Property setPort() As String
        Get
            Return _PortNumberString
        End Get
        Set(ByVal value As String)
            _PortNumberString = value
        End Set
    End Property
Now I put the call in the IP address since that's the first property that gets written (it's not really the correct way to do it, you should call the method from the program and pass the arguments that way, but this works just as well.)

Method
Code:
    Public Sub SaveSettings()

        Dim SaveWriter As XmlWriter = XmlWriter.Create("Settings.xml")
        SaveWriter.WriteStartElement("Settings")
        SaveWriter.WriteElementString("IPADDRESS", _IPaddressString)
        SaveWriter.WriteEndElement()
        SaveWriter.Flush()

    End Sub
This sets up the xml file and saves it to where the program is currently located. _IPaddress is the variable we used in the property which is set in the program.

To use this class, on your form you need to instantiate the class and make a Module level variable.

Code:
Dim ixmlsettings As settingsClass
Now for the button click event we are going to make it a new instance of the settingsClass

Code:
    Private Sub btn_Save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Save.Click
        Dim Ipaddress As String
        Ipaddress = txt_IPAddress.Text.Replace(" ", "")

        ixmlsettings = New settingsClass
        ixmlsettings.setIPAddress = txt_IPAddress.Text


    End Sub
I used a masked textbox to give a format for the user to enter so the txt_IPAddress.Text.Replace is just removing the spaces.

ixmlsettings.setIPAddress = txt_IPAddress.Text
Is setting the setIPAddress property which will then fire the method to write out to the file.

So for my program it looks a little something like this:


The outcome being: