我注意到我的雾虫报告中有很多“无法访问文件,因为它正被另一个进程使用”错误。我猜这可能与文件打开后未关闭有关。或者保存后不被关闭。任何人都可以验证这是否是我的问题并建议我更好的解决方法。
在表单加载时打开此文件,并在关闭时保存文件。
表单加载
If IO.File.Exists(myCoolFile) Then '// check if file exists.
Dim myCoolFileLines() As String = IO.File.ReadAllLines(myCoolFile) '// load your file as a string array.
For Each line As String In myCoolFileLines '// loop thru array list.
Dim lineArray() As String = line.Split("#") '// separate by "#" character.
'Dim newItem As New ListViewItem(lineArray(0)) '// add text Item.
' ListView1.Items.Add(newItem) '// add Item to ListView.
ListView1.Items.Add(lineArray(0)).Tag = (lineArray(1))
Next
Else
If Not File.Exists(myCoolFile) Then
File.Create(myCoolFile)
End If
表格关闭
Dim myWriter As New IO.StreamWriter(myCoolFile)
For Each myItem As ListViewItem In ListView1.Items
myWriter.WriteLine(myItem.Text & "#" & myItem.Tag) '// write Item and SubItem.
Next
myWriter.Close()