当前位置 : 主页 > 编程语言 > c语言 >

vb.net 读写ini文件示例

来源:互联网 收集:自由互联 发布时间:2021-06-24
Imports System.Collections.GenericImports System.TextImports System.Runtime.InteropServicesPublic Class IniFile Public filePath As String DllImport("kernel32") _ Private Shared Function WritePrivateProfileString(ByVal section As String, ByV
Imports System.Collections.Generic
Imports System.Text
Imports System.Runtime.InteropServices

Public Class IniFile

    Public filePath As String
    <DllImport("kernel32")> _
    Private Shared Function WritePrivateProfileString(ByVal section As String, ByVal key As String, ByVal val As String, ByVal filePath As String) As Long
    End Function
    <DllImport("kernel32")> _
    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文件(*.ini)|*.ini"
        If OpenFileDialog.ShowDialog = DialogResult.OK Then
            Dim filePath As String = OpenFileDialog.FileName
            If Not filePath.EndsWith(".ini") Then

                MsgBox("请选择以INI结尾的文件")

            End If
            ‘读取配置文件
            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


        ‘保存配置文件
        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
网友评论