1. vb.net 读取/写入文件
参考地址: http://www.jb51.net/article/15531.htm
主要是需要借助System.IO 的 streamReader 和 streamWriter 方法, 实现文件的读取和写入
文件写入
StandardExpoSettingPath = "D:\\LinLan\\standard_expo_setting.ini"
Dim writer2 As StreamWriter = New StreamWriter(StandardExpoSettingPath, False)
str = CStr(light_upper) + vbTab + CStr(light_lower) + vbTab + CStr(dark_upper) + vbTab + CStr(dark_lower)
writer2.WriteLine(str)
writer2.Flush()
writer2.Close()
writer2 = Nothing
文件读取
Try
Dim reader6 As StreamReader
reader6 = File.OpenText(PaituPath)
Dim str As String
str = reader6.ReadLine()
If str = "No" Then
will_do_next.Checked = False
Else
will_do_next.Checked = True
End If
reader6.Close()
Catch ex As Exception
End Try
2. 实现效果
3. 工程地址
https://code.csdn.net/zhyh1435589631/lab_camera_setting_ui/tree/master
4. 实现代码
Imports System.IO
Public Class Form1
' 自动曝光度调节
Dim AutoExpoSettingPath As String
Dim left_low_value As Integer
Dim left_high_value As Integer
Dim right_low_value As Integer
Dim right_high_value As Integer
Dim left_gap_value As Integer
Dim right_gap_value As Integer
' 标准参数调节
Dim StandardExpoSettingPath As String
Dim light_upper As Integer
Dim light_lower As Integer
Dim dark_upper As Integer
Dim dark_lower As Integer
' 解码算点
Dim WmatrixPath As String
Dim AsPath As String
Dim k3rPath As String
Dim PaituPath As String
Private Sub TB_left_low_TextChanged(sender As Object, e As EventArgs) Handles TB_left_low.TextChanged
Try
left_low_value = CInt(TB_left_low.Text.ToString)
Catch ex As Exception
End Try
End Sub
Private Sub TB_left_high_TextChanged(sender As Object, e As EventArgs) Handles TB_left_high.TextChanged
Try
left_high_value = CInt(TB_left_high.Text.ToString)
Catch ex As Exception
End Try
End Sub
Private Sub TB_right_low_TextChanged(sender As Object, e As EventArgs) Handles TB_right_low.TextChanged
Try
right_low_value = CInt(TB_right_low.Text.ToString)
Catch ex As Exception
End Try
End Sub
Private Sub TB_right_high_TextChanged(sender As Object, e As EventArgs) Handles TB_right_high.TextChanged
Try
right_high_value = CInt(TB_right_high.Text.ToString)
Catch ex As Exception
End Try
End Sub
Private Sub TB_left_gap_TextChanged(sender As Object, e As EventArgs) Handles TB_left_gap.TextChanged
Try
left_gap_value = CInt(TB_left_gap.Text.ToString)
Catch ex As Exception
End Try
End Sub
Private Sub TB_right_gap_TextChanged(sender As Object, e As EventArgs) Handles TB_right_gap.TextChanged
Try
right_gap_value = CInt(TB_right_gap.Text.ToString)
Catch ex As Exception
End Try
End Sub
Private Sub OK_Click(sender As Object, e As EventArgs) Handles OK.Click
' 自动曝光度调节部分
AutoExpoSettingPath = "D:\\LinLan\\auto_expo_setting.ini"
Dim writer As StreamWriter = New StreamWriter(AutoExpoSettingPath, False)
Dim str As String
str = CStr(left_low_value) + vbTab + CStr(left_high_value) + vbTab + CStr(right_low_value) + vbTab + CStr(right_high_value) + vbTab + CStr(left_gap_value) + vbTab + CStr(right_gap_value)
writer.WriteLine(str)
writer.Flush()
writer.Close()
writer = Nothing
' 标准参数配置
StandardExpoSettingPath = "D:\\LinLan\\standard_expo_setting.ini"
Dim writer2 As StreamWriter = New StreamWriter(StandardExpoSettingPath, False)
str = CStr(light_upper) + vbTab + CStr(light_lower) + vbTab + CStr(dark_upper) + vbTab + CStr(dark_lower)
writer2.WriteLine(str)
writer2.Flush()
writer2.Close()
writer2 = Nothing
'解码算点
PaituPath = "D:\\LinLan\\paitu.ini"
Dim writer3 As StreamWriter = New StreamWriter(PaituPath, False)
Dim checked As Boolean
checked = will_do_next.Checked
If checked Then
str = "Yes"
Else
str = "No"
End If
writer3.WriteLine(str)
writer3.Flush()
writer3.Close()
writer3 = Nothing
Close()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' 自动曝光度参数调节
AutoExpoSettingPath = "D:\\LinLan\\auto_expo_setting.ini"
Dim arr_str As String()
Dim reader As StreamReader
Try
reader = File.OpenText(AutoExpoSettingPath)
Dim str As String
str = reader.ReadLine()
arr_str = str.Split()
reader.Close()
Catch ex As Exception
arr_str = {"0", "0", "0", "0", "0", "0"}
End Try
TB_left_low.Text = arr_str(0)
TB_left_high.Text = arr_str(1)
TB_right_low.Text = arr_str(2)
TB_right_high.Text = arr_str(3)
TB_left_gap.Text = arr_str(4)
TB_right_gap.Text = arr_str(5)
' 标准参数配置
StandardExpoSettingPath = "D:\\LinLan\\standard_expo_setting.ini"
Dim stand_arr_str As String()
Try
Dim reader2 As StreamReader
reader2 = File.OpenText(StandardExpoSettingPath)
Dim str As String
str = reader2.ReadLine()
stand_arr_str = str.Split()
reader2.Close()
Catch ex As Exception
stand_arr_str = {"0", "0", "0", "0"}
End Try
TB_light_upper.Text = stand_arr_str(0)
TB_light_low.Text = stand_arr_str(1)
TB_dark_high.Text = stand_arr_str(2)
TB_dark_low.Text = stand_arr_str(3)
' 解码算点
WmatrixPath = "D:\\LinLan\\wmatrix.ini"
AsPath = "D:\\LinLan\\as.ini"
k3rPath = "D:\\LinLan\\k3r.ini"
Try
Dim reader3 As StreamReader
reader3 = File.OpenText(WmatrixPath)
TB_Wmatrix.Text = reader3.ReadLine()
reader3.Close()
Dim reader4 As StreamReader
reader4 = File.OpenText(AsPath)
TB_As_k3r.Text = reader4.ReadLine()
reader4.Close()
Dim reader5 As StreamReader
reader5 = File.OpenText(k3rPath)
TB_k3r.Text = reader5.ReadLine()
reader5.Close()
Catch ex As Exception
End Try
PaituPath = "D:\\LinLan\\paitu.ini"
Try
Dim reader6 As StreamReader
reader6 = File.OpenText(PaituPath)
Dim str As String
str = reader6.ReadLine()
If str = "No" Then
will_do_next.Checked = False
Else
will_do_next.Checked = True
End If
reader6.Close()
Catch ex As Exception
End Try
End Sub
Private Sub TB_light_upper_TextChanged(sender As Object, e As EventArgs) Handles TB_light_upper.TextChanged
Try
light_upper = CInt(TB_light_upper.Text.ToString)
Catch ex As Exception
End Try
End Sub
Private Sub TB_light_low_TextChanged(sender As Object, e As EventArgs) Handles TB_light_low.TextChanged
Try
light_lower = CInt(TB_light_low.Text.ToString)
Catch ex As Exception
End Try
End Sub
Private Sub TB_dark_high_TextChanged(sender As Object, e As EventArgs) Handles TB_dark_high.TextChanged
Try
dark_upper = CInt(TB_dark_high.Text.ToString)
Catch ex As Exception
End Try
End Sub
Private Sub TB_dark_low_TextChanged(sender As Object, e As EventArgs) Handles TB_dark_low.TextChanged
Try
dark_lower = CInt(TB_dark_low.Text.ToString)
Catch ex As Exception
End Try
End Sub
Private Sub TB_Wmatrix_MouseDoubleClick(sender As Object, e As MouseEventArgs) Handles TB_Wmatrix.MouseDoubleClick
WmatrixPath = "D:\\LinLan\\wmatrix.ini"
OpenWmatrix.InitialDirectory = "C:\"
OpenWmatrix.Filter = "txt files (*.txt) | *.txt | All files (*.*)|*.*"
OpenWmatrix.FilterIndex = 2
OpenWmatrix.RestoreDirectory = True
If OpenWmatrix.ShowDialog() = DialogResult.OK Then
TB_Wmatrix.Text = OpenWmatrix.FileName()
Dim writer As StreamWriter = New StreamWriter(WmatrixPath, False)
Dim str As String
str = TB_Wmatrix.Text
writer.WriteLine(str)
writer.Flush()
writer.Close()
writer = Nothing
End If
End Sub
Private Sub TB_As_k3r_MouseDoubleClick(sender As Object, e As MouseEventArgs) Handles TB_As_k3r.MouseDoubleClick
AsPath = "D:\\LinLan\\as.ini"
OpenAs.InitialDirectory = "C:\"
OpenAs.Filter = "txt files (*.txt) | *.txt | All files (*.*)|*.*"
OpenAs.FilterIndex = 2
OpenAs.RestoreDirectory = True
If OpenAs.ShowDialog() = DialogResult.OK Then
TB_As_k3r.Text = OpenAs.FileName()
Dim writer As StreamWriter = New StreamWriter(AsPath, False)
Dim str As String
str = TB_As_k3r.Text
writer.WriteLine(str)
writer.Flush()
writer.Close()
writer = Nothing
End If
End Sub
Private Sub TB_k3r_MouseDoubleClick(sender As Object, e As MouseEventArgs) Handles TB_k3r.MouseDoubleClick
k3rPath = "D:\\LinLan\\k3r.ini"
Openk3r.InitialDirectory = "C:\"
Openk3r.Filter = "txt files (*.txt) | *.txt | All files (*.*)|*.*"
Openk3r.FilterIndex = 2
Openk3r.RestoreDirectory = True
If Openk3r.ShowDialog() = DialogResult.OK Then
TB_k3r.Text = Openk3r.FileName()
Dim writer As StreamWriter = New StreamWriter(k3rPath, False)
Dim str As String
str = TB_k3r.Text
writer.WriteLine(str)
writer.Flush()
writer.Close()
writer = Nothing
End If
End Sub
End Class