VB.NET reads and writes an ini file example

Imports System.Collections.Generic
Imports System.Text
Imports System.Runtime.InteropServices

Public Class IniFile

    Public filePath As String
     _
    Private Shared Function WritePrivateProfileString(ByVal section As String, ByVal key As String, ByVal val As String, ByVal filePath As String) As Long
    End Function
     _
    Private Shared Function GetPrivateProfileString(ByVal section As String, ByVal key As String, ByVal def As String, ByVal retVal As StringBuilder, ByVal size As Integer, ByVal filePath As String) As Integer
    End Function
    Public Sub New(ByVal iniPath As String)
        filePath = iniPath
    End Sub
    Public Sub WriteIniValue(ByVal Section As String, ByVal Key As String, ByVal value As String)
        WritePrivateProfileString(Section, Key, value, Me.filePath)
    End Sub
    Public Function ReadIniValue(ByVal Section As String, ByVal Key As String) As String
        Dim temp As New StringBuilder(255)
        Dim i As Integer = GetPrivateProfileString(Section, Key, "", temp, 255, Me.filePath)
        Return temp.ToString()
    End Function

End Class

  

 Private Sub bt_read_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt_read.Click
        OpenFileDialog.Filter = "ini file (*.ini)|*.ini"
        If OpenFileDialog.ShowDialog = DialogResult.OK Then
            Dim filePath As String = OpenFileDialog.FileName
            If Not filePath.EndsWith(".ini") Then

                MsgBox("Please select a file ending in INI")

            End If
            ‘Read configuration file
            fileIni = New IniFile(filePath)
            cmb_id.Text = fileIni.ReadIniValue("Implants", "id")
            cmb_use.Text = fileIni.ReadIniValue("Implants", "use")
            cmb_chnum.Text = fileIni.ReadIniValue("Implants", "Chnum")
            tb_mark.Text = fileIni.ReadIniValue("Implants", "Mark")
            tb_devid.Text = fileIni.ReadIniValue("Implants", "DevId")
            tb_modid.Text = fileIni.ReadIniValue("Implants", "ModId")
            cmb_timenum.Text = fileIni.ReadIniValue("Implants", "Timenum")
            Dim section As String = "CH"
            ‘(This.Controls["label1" + s.ToString()] as Label).xxx
            For i As Integer = 1 To fileIni.ReadIniValue("Implants", "Chnum")
                Me.Controls("gb_implantsChannleSetting" & i).Controls("cmb_gain" & i).Text = fileIni.ReadIniValue(section & i, "gain")
                Me.Controls("gb_implantsChannleSetting" & i).Controls("cmb_pole" & i).Text = fileIni.ReadIniValue(section & i, "pole")
                Me.Controls("gb_implantsChannleSetting" & i).Controls("tb_zero" & i).Text = fileIni.ReadIniValue(section & i, "Zero")
                Me.Controls("gb_implantsChannleSetting" & i).Controls("tb_range" & i).Text = fileIni.ReadIniValue(section & i, "Range")
                Me.Controls("gb_implantsChannleSetting" & i).Controls("tb_clear" & i).Text = fileIni.ReadIniValue(section & i, "Clear")
                Me.Controls("gb_implantsChannleSetting" & i).Controls("tb_v_u" & i).Text = fileIni.ReadIniValue(section & i, "V/U")
            Next

        End If





    End Sub

  

 Private Sub bt_save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt_save.Click


        ‘Save configuration file
        Dim filePath As String = "setting.ini"
        If FolderBrowserDialog.ShowDialog = DialogResult.OK Then

            filePath = FolderBrowserDialog.SelectedPath & "\" & filePath

            fileIni = New IniFile(filePath)
            fileIni.WriteIniValue("Implants", "id", cmb_id.Text)
            fileIni.WriteIniValue("Implants", "use", cmb_use.Text)
            fileIni.WriteIniValue("Implants", "Chnum", cmb_chnum.Text)
            fileIni.WriteIniValue("Implants", "Mark", "ox55")
            fileIni.WriteIniValue("Implants", "DevId", tb_devid.Text)
            fileIni.WriteIniValue("Implants", "ModId", tb_modid.Text)
            fileIni.WriteIniValue("Implants", "Timenum", cmb_timenum.Text)

            fileIni.WriteIniValue("CH1", "gain", cmb_gain1.Text)
            fileIni.WriteIniValue("CH1", "pole", cmb_pole1.Text)
            fileIni.WriteIniValue("CH1", "Zero", tb_zero1.Text)
            fileIni.WriteIniValue("CH1", "Range", tb_range1.Text)
            fileIni.WriteIniValue("CH1", "Clear", tb_clear1.Text)
            fileIni.WriteIniValue("CH1", "V/U", tb_v_u1.Text)

            fileIni.WriteIniValue("CH2", "gain", cmb_gain2.Text)
            fileIni.WriteIniValue("CH2", "pole", cmb_pole2.Text)
            fileIni.WriteIniValue("CH2", "Zero", tb_zero2.Text)
            fileIni.WriteIniValue("CH2", "Range", tb_range2.Text)
            fileIni.WriteIniValue("CH2", "Clear", tb_clear2.Text)
            fileIni.WriteIniValue("CH2", "V/U", tb_v_u2.Text)

            fileIni.WriteIniValue("CH3", "gain", cmb_gain3.Text)
            fileIni.WriteIniValue("CH3", "pole", cmb_pole3.Text)
            fileIni.WriteIniValue("CH3", "Zero", tb_zero3.Text)
            fileIni.WriteIniValue("CH3", "Range", tb_range3.Text)
            fileIni.WriteIniValue("CH3", "Clear", tb_clear3.Text)
            fileIni.WriteIniValue("CH3", "V/U", tb_v_u3.Text)

            fileIni.WriteIniValue("CH4", "gain", cmb_gain4.Text)
            fileIni.WriteIniValue("CH4", "pole", cmb_pole4.Text)
            fileIni.WriteIniValue("CH4", "Zero", tb_zero4.Text)
            fileIni.WriteIniValue("CH4", "Range", tb_range4.Text)
            fileIni.WriteIniValue("CH4", "Clear", tb_clear4.Text)
            fileIni.WriteIniValue("CH4", "V/U", tb_v_u4.Text)

        End If

Imports System.Collections.Generic
Imports System.Text
Imports System.Runtime.InteropServices

Public Class IniFile

    Public filePath As String
     _
    Private Shared Function WritePrivateProfileString(ByVal section As String, ByVal key As String, ByVal val As String, ByVal filePath As String) As Long
    End Function
     _
    Private Shared Function GetPrivateProfileString(ByVal section As String, ByVal key As String, ByVal def As String, ByVal retVal As StringBuilder, ByVal size As Integer, ByVal filePath As String) As Integer
    End Function
    Public Sub New(ByVal iniPath As String)
        filePath = iniPath
    End Sub
    Public Sub WriteIniValue(ByVal Section As String, ByVal Key As String, ByVal value As String)
        WritePrivateProfileString(Section, Key, value, Me.filePath)
    End Sub
    Public Function ReadIniValue(ByVal Section As String, ByVal Key As String) As String
        Dim temp As New StringBuilder(255)
        Dim i As Integer = GetPrivateProfileString(Section, Key, "", temp, 255, Me.filePath)
        Return temp.ToString()
    End Function

End Class

 Private Sub bt_read_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt_read.Click
        OpenFileDialog.Filter = "ini file (*.ini)|*.ini"
        If OpenFileDialog.ShowDialog = DialogResult.OK Then
            Dim filePath As String = OpenFileDialog.FileName
            If Not filePath.EndsWith(".ini") Then

                MsgBox("Please select a file ending in INI")

            End If
            ‘Read configuration file
            fileIni = New IniFile(filePath)
            cmb_id.Text = fileIni.ReadIniValue("Implants", "id")
            cmb_use.Text = fileIni.ReadIniValue("Implants", "use")
            cmb_chnum.Text = fileIni.ReadIniValue("Implants", "Chnum")
            tb_mark.Text = fileIni.ReadIniValue("Implants", "Mark")
            tb_devid.Text = fileIni.ReadIniValue("Implants", "DevId")
            tb_modid.Text = fileIni.ReadIniValue("Implants", "ModId")
            cmb_timenum.Text = fileIni.ReadIniValue("Implants", "Timenum")
            Dim section As String = "CH"
            ‘(This.Controls["label1" + s.ToString()] as Label).xxx
            For i As Integer = 1 To fileIni.ReadIniValue("Implants", "Chnum")
                Me.Controls("gb_implantsChannleSetting" & i).Controls("cmb_gain" & i).Text = fileIni.ReadIniValue(section & i, "gain")
                Me.Controls("gb_implantsChannleSetting" & i).Controls("cmb_pole" & i).Text = fileIni.ReadIniValue(section & i, "pole")
                Me.Controls("gb_implantsChannleSetting" & i).Controls("tb_zero" & i).Text = fileIni.ReadIniValue(section & i, "Zero")
                Me.Controls("gb_implantsChannleSetting" & i).Controls("tb_range" & i).Text = fileIni.ReadIniValue(section & i, "Range")
                Me.Controls("gb_implantsChannleSetting" & i).Controls("tb_clear" & i).Text = fileIni.ReadIniValue(section & i, "Clear")
                Me.Controls("gb_implantsChannleSetting" & i).Controls("tb_v_u" & i).Text = fileIni.ReadIniValue(section & i, "V/U")
            Next

        End If





    End Sub

 Private Sub bt_save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt_save.Click


        ‘Save configuration file
        Dim filePath As String = "setting.ini"
        If FolderBrowserDialog.ShowDialog = DialogResult.OK Then

            filePath = FolderBrowserDialog.SelectedPath & "\" & filePath

            fileIni = New IniFile(filePath)
            fileIni.WriteIniValue("Implants", "id", cmb_id.Text)
            fileIni.WriteIniValue("Implants", "use", cmb_use.Text)
            fileIni.WriteIniValue("Implants", "Chnum", cmb_chnum.Text)
            fileIni.WriteIniValue("Implants", "Mark", "ox55")
            fileIni.WriteIniValue("Implants", "DevId", tb_devid.Text)
            fileIni.WriteIniValue("Implants", "ModId", tb_modid.Text)
            fileIni.WriteIniValue("Implants", "Timenum", cmb_timenum.Text)

            fileIni.WriteIniValue("CH1", "gain", cmb_gain1.Text)
            fileIni.WriteIniValue("CH1", "pole", cmb_pole1.Text)
            fileIni.WriteIniValue("CH1", "Zero", tb_zero1.Text)
            fileIni.WriteIniValue("CH1", "Range", tb_range1.Text)
            fileIni.WriteIniValue("CH1", "Clear", tb_clear1.Text)
            fileIni.WriteIniValue("CH1", "V/U", tb_v_u1.Text)

            fileIni.WriteIniValue("CH2", "gain", cmb_gain2.Text)
            fileIni.WriteIniValue("CH2", "pole", cmb_pole2.Text)
            fileIni.WriteIniValue("CH2", "Zero", tb_zero2.Text)
            fileIni.WriteIniValue("CH2", "Range", tb_range2.Text)
            fileIni.WriteIniValue("CH2", "Clear", tb_clear2.Text)
            fileIni.WriteIniValue("CH2", "V/U", tb_v_u2.Text)

            fileIni.WriteIniValue("CH3", "gain", cmb_gain3.Text)
            fileIni.WriteIniValue("CH3", "pole", cmb_pole3.Text)
            fileIni.WriteIniValue("CH3", "Zero", tb_zero3.Text)
            fileIni.WriteIniValue("CH3", "Range", tb_range3.Text)
            fileIni.WriteIniValue("CH3", "Clear", tb_clear3.Text)
            fileIni.WriteIniValue("CH3", "V/U", tb_v_u3.Text)

            fileIni.WriteIniValue("CH4", "gain", cmb_gain4.Text)
            fileIni.WriteIniValue("CH4", "pole", cmb_pole4.Text)
            fileIni.WriteIniValue("CH4", "Zero", tb_zero4.Text)
            fileIni.WriteIniValue("CH4", "Range", tb_range4.Text)
            fileIni.WriteIniValue("CH4", "Clear", tb_clear4.Text)
            fileIni.WriteIniValue("CH4", "V/U", tb_v_u4.Text)

        End If

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 3408 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.