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