我試圖刪除選定的行,然後將其餘的保存到文件中。但是,當我保存它時,它完全清空文件。我的列表(字符串)正在保存爲system.string(空)
Console.Write("Please eneter the first name of the student you wish to search for: ")
searchfname = Console.ReadLine
searchfname = StrConv(searchfname, VbStrConv.ProperCase)
Console.Write("Please enter the second name of the student you wish to search for: ")
searchsname = Console.ReadLine
searchsname = StrConv(searchsname, VbStrConv.ProperCase)
Dim foundItem() As String = Nothing
Dim foundline As String = Nothing
Dim fnsearch As String = String.Join(searchfname, searchsname)
Dim lines As New List(Of String)(File.ReadAllLines("F:\Computing\Spelling Bee\stdnt&staffdtls.csv"))
For Each line As String In lines
If searchfname = item(3) And searchsname = item(4) Then
Console.WriteLine(line)
Console.WriteLine()
Console.WriteLine("Are you sure you wish to delete this record? (y/n)")
End If
Dim answer As String
answer = Console.ReadLine
If answer = "y" Or answer = "Y" Then
Console.Clear()
lines.Remove(line)
Using sw As New StreamWriter("F:\Computing\Spelling Bee\stdnt&staffdtls.csv")
sw.WriteLine(lines.ToString)
End Using
ElseIf answer = "n" Or answer = "N" Then
staffmenu()
End If
Next
您沒有正確寫入文件。改爲使用File.WriteAllLines()。 –
這裏有幾個非常好的答案 - 以及以前的7個問題。你應該在他們幫忙時開始接受答案。 – Plutonix
與您的問題無關,而是您對JoeBlogs或Joe Blogs的搜索....如果後者改變您的連接,以便它包含searchfname和searchsname之間的空格 – Mych