CHECKBOX状态变化时,将记录写入INI文件

Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
' 配置文件路径
Dim configFilePath As String = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "config.ini")

    ' 读取配置文件
    Dim fileContents As String = File.ReadAllText(configFilePath)

    ' 查找参数的位置
    Dim serverParamIndex As Integer = fileContents.IndexOf("NewForm=")

    ' 如果找到了参数
    If serverParamIndex >= 0 Then
        ' 计算Server参数值的结束位置
        Dim valueStartIndex As Integer = serverParamIndex + "NewForm=".Length
        Dim valueEndIndex As Integer = fileContents.IndexOf(vbCrLf, valueStartIndex)

        ' 如果找到了值的结束位置
        If valueEndIndex >= 0 Then
            ' 替换Server参数的值
            fileContents = fileContents.Substring(0, valueStartIndex) & CheckBox1.Checked & fileContents.Substring(valueEndIndex)
        Else
            ' 如果没有找到值的结束位置,直接在Server参数后添加123
            fileContents = fileContents.Insert(serverParamIndex + "NewForm=".Length, CheckBox1.Checked)
        End If
    Else
        ' 如果没有找到Server参数,添加一个新的Server参数
        fileContents &= vbCrLf & "NewForm=" & CheckBox1.Checked
    End If

    ' 将修改后的配置写回文件
    File.WriteAllText(configFilePath, fileContents)
End Sub

'这是创建INI文件的代码,加入LOAD中
Sub CreateConfigFile(filePath As String)
Using writer As New StreamWriter(filePath)
writer.WriteLine("NewForm=False")
End Using
End Sub

'这是使用的代码
Dim checked = False
Dim filePath As String = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "config.ini")
If File.Exists(filePath) Then
CheckBox1.Checked = ReadiniValue(filePath)
Else
CreateConfigFile(filePath)
End If

posted @ 2024-12-20 07:50  令勾子  阅读(14)  评论(0)    收藏  举报