editing a config text file with checkboxes

Nucleus

Active member
Joined
May 24, 2005
Messages
30
Programming Experience
Beginner
Hi,

I want to create a Windows form with checkboxes, which will edit the configuration file of apache webserver. That’s a 437 lines text file, which means that each time I want to edit a setting, visual basic will have to find the correct line and make the appropriate modification.

I have have this code so far, that opens the file, and after the modifications, saves it.

VB.NET:
Dim f As Integer
Dim s As String

  
  'load the whole file into a string variable
  f = FreeFile()
  Open "<filename>" For Binary As #f
  s = Space$(LOF(f))
  Get #f, , s
  Close #f

  
  'do the modifications here

  
  
  'save the string variable into the file
  Kill "<filename>"
  f = FreeFile()
  Open "<filename>" For Binary As #f
  Put #f, , s
  Close #f

But I still found no way of actually going to a specific line and making the change.
 
While working on a text file, i got this code to work half way.

VB.NET:
Public Class Form1
    Dim Lines() As String = System.IO.File.ReadAllLines("C:\test.txt")
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Save.Click

        If ModActions.Enabled = True Then
            Lines(0) = "selected"
            System.IO.File.WriteAllLines("C:\test.txt", Lines)
        ElseIf ModActions.Enabled = False Then
            Lines(0) = "unselected"
            System.IO.File.WriteAllLines("C:\test.txt", Lines)
        End If

    End Sub
End Class

"unselected" doesnt seem to appear though
 
is ModActions supposed to be the checkbox?

changed...

VB.NET:
Public Class Form1
    Dim Lines() As String = System.IO.File.ReadAllLines("C:\test.txt")
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Save.Click

        If ModActions.Checked = True Then
            Lines(0) = "selected"
            System.IO.File.WriteAllLines("C:\test.txt", Lines)
        Else
            Lines(0) = "unselected"
            System.IO.File.WriteAllLines("C:\test.txt", Lines)
        End If

    End Sub
End Class
 
Thanks for your reply. Can you also show me how to read that line from the text file, so that i will know if the checkbox is selected or not when the form opens?

Thanks.
 
Well in form_load you could do...

VB.NET:
    Dim lines() As String = System.IO.File.ReadAllLines("C:\test.txt")

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        If lines(0).StartsWith("selected") Then
            ModActions.Checked = True
        Else
            ModActions.Checked = False
        End If
    End Sub

Why exactly do you want to write "Selected" or "Unselected" to the file? You could use My.Settings to set a boolean flag.
 
I am creating a form to edit the configuration file of apache web server.

The 2 modules I started with are mod_actions and mod_alias.

VB.NET:
Public Class ApacheSettings
    Dim Lines() As String = System.IO.File.ReadAllLines("C:\webserver\Apache2.2\conf\httpd.conf")

    Private Sub ApacheSettings_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        If Lines(53).StartsWith("LoadModule actions_module modules/mod_actions.so") Then
            ApacheModActions.Checked = True
        Else
            ApacheModActions.Checked = False
        End If

        If Lines(54).StartsWith("LoadModule alias_module modules/mod_alias.so") Then
            ApacheModAlias.Checked = True
        Else
            ApacheModAlias.Checked = False
        End If

    End Sub

    Private Sub Save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Save.Click

        If ApacheModActions.Checked = True Then
            Lines(53) = "LoadModule actions_module modules/mod_actions.so"
            System.IO.File.WriteAllLines("C:\webserver\Apache2.2\conf\httpd.conf", Lines)
        Else
            Lines(53) = "#LoadModule actions_module modules/mod_actions.so"
            System.IO.File.WriteAllLines("C:\webserver\Apache2.2\conf\httpd.conf", Lines)
        End If

    End Sub

    Private Sub ApacheModAlias_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ApacheModAlias.CheckedChanged

        If ApacheModAlias.Checked = True Then
            Lines(54) = "LoadModule alias_module modules/mod_alias.so"
            System.IO.File.WriteAllLines("C:\webserver\Apache2.2\conf\httpd.conf", Lines)
        Else
            Lines(54) = "#LoadModule alias_module modules/mod_alias.so"
            System.IO.File.WriteAllLines("C:\webserver\Apache2.2\conf\httpd.conf", Lines)
        End If

    End Sub
End Class
 
Instead of reading and writing to the file directly every time, it would probably be easier (and faster) to read the whole config file, assign configuration values to a list, modify the list as you please and then write it all back. Since you want to look for them by name, you could use a System.Data.Datatable object to hold the config file in memory. For example:

Apache config says:
VB.NET:
ServerName server.domain.com
DocumentRoot /www/mainserver
NameVirtualHost 172.20.30.50
<VirtualHost 172.20.30.50>
     DocumentRoot /www/example1
     ServerName www.example1.com
</VirtualHost>

Create a table with columns ParameterName, ParameterValue, and Section:
VB.NET:
ParameterName       ParameterValue        SectionName
"ServerName"        "server.domain.com"
"DocumentRoot"      "/www/mainserver"
"NameVirtualHost"   "172.20.30.50"
"DocumentRoot"      "/www/example1"       "VirtualHost 172.20.30.50"
"ServerName"        "www.example1.com"    "VirtualHost 172.20.30.50"

Now you can bind your controls to specific fields. Once the new config is accepted, hit save and rewrite the apache config file from your table.
 
Last edited:
That would actually be very helpful because the codeblock is getting very big as i continue with the apache modules. Can you provide an example of how the code would look like with your suggestion? (just getting started with vb)
 
Here is a basic framework:

    Private ConfigTable As System.Data.DataTable

    Private Sub ParseApacheConfig(Filename As String)
        Dim Lines() As String = System.IO.File.ReadAllLines(Filename)
        Dim Results(3) As String

        Dim Columns(3) As System.Data.DataColumn
        Columns(0) = New System.Data.DataColumn With {.ColumnName = "ParameterName"}
        Columns(1) = New System.Data.DataColumn With {.ColumnName = "ParameterValue"}
        Columns(2) = New System.Data.DataColumn With {.ColumnName = "SectionName"}

        ConfigTable = New System.Data.DataTable
        ConfigTable.Columns.AddRange(Columns)

        For Each Line As String In Lines
            ' Parse the configuration file syntax line by line here
            ' Extract the configuration name and value
            ' Detect beginning and ending of Sections and flag appropriately
            ' Put results in the Results array then into the datatable
            ConfigTable.Rows.Add(Results)
        Next
    End Sub


    Private Function GetSetParamValue(ParamName As String, SectionName As String, ParamValue As String) As String
        Dim Result() As System.Data.DataRow = ConfigTable.Select("ParameterName = '" & ParamName & "' And " & _
                                                                 "SectionName = '" & SectionName & "'")
        ' You would probably need to account for multiple rows from a select like this for debugging.
        If Not ParamValue = "" Then
            Result(0).Item("ParameterValue") = ParamValue
            Result(0).Table.AcceptChanges()
        End If
        GetSetParamValue = Result(0).Item("ParameterValue").ToString
    End Function


To retrieve a parameter value:

Dim Value As String = GetSetParamValue("ServerName", "VirtualHost 172.20.30.50", "")


To set a parameter value:

GetSetParamValue "ServerName", "VirtualHost 172.20.30.50", "www.foo.com"
 
Last edited:
Back
Top