取代我已經看到了這裏幾次這個問題,但我似乎無法找到正確的答案。我目前有一個Windows窗體應用程序,我用VB編寫,創建玩家配置文件(這是一個學校項目)。在'註冊'頁面上,我將這些信息讀取到一個.txt文件中,每行用逗號分隔符逐行顯示。這使得一個.txt,看起來是這樣的:在一個.txt文件中刪除特定行,並與新的生產線vb.net
Dan,Boyle,M,39,(707) 555-1234,555 S. West Street,18,D,R
Justin,Braun,M,27,(916) 555-1255,858 W. South St,5,D,L
我跑到右邊線得到這一點,稱這只是罰款,但我需要爲我的項目做的是能夠編輯球員的個人資料。到目前爲止,我發現最好的辦法是重寫整個系列,這是我設法做到的。但我不明白的是如何刪除當前行並進行替換。
Dim FileString As String = "MasterRoster.txt"
Dim index As Integer = Home.cbPlayerLookUp.SelectedIndex
Dim playerInfo() As String = File.ReadAllLines(FileString)
Dim FieldString() As String
Dim RecordString As String = ""
If Not playerInfo.Length < index Then
FieldString = playerInfo(index).Split(","c)
'Update Player stats)
If CInt(FieldString(16)) = index Then
FieldString(12) = CStr(CInt(FieldString(12)) + CInt(txtGoals.Text))
FieldString(13) = CStr(CInt(FieldString(13)) + CInt(txtAssists.Text))
FieldString(14) = CStr(CInt(FieldString(14)) + CInt(txtPIM.Text))
FieldString(15) = CStr(CInt(FieldString(15)) + CInt(txtGames.Text))
'(FirstName0, LastName1, Gender2, Age3, Phone4, Address5, Experience6, Position7, Handedness8, Team9, Jersey#10, imgPath11, Goals12, Assists13, PIM14, Total Games15, playerID Number16)
RecordString = FieldString(0) & "," & FieldString(1) & "," & FieldString(2) & "," & FieldString(3) & "," & FieldString(4) & "," & FieldString(5) &
"," & FieldString(6) & "," & FieldString(7) & "," & FieldString(8) & "," & FieldString(9) & "," & FieldString(10) & "," & FieldString(11) & "," & FieldString(12) & "," &
FieldString(13) & "," & FieldString(14) & "," & FieldString(15) & "," & FieldString(16) & vbNewLine
End If
My.Computer.FileSystem.WriteAllText(FileString, RecordString, True)
End If
目前這將添加一個新行到我的.txt文件「MasterRoster.txt」,並在最後的更新信息的底部,但舊的仍然存在。我怎樣才能取代舊的線?
文本CSV文件不是隨機訪問。您必須替換跳過舊記錄並添加新記錄的文件。 – Plutonix
得到了雅。所以...我想需要在舊文件中讀取,改變什麼,我想改變,並重新打印 – user2788749
,因爲你明顯不具備加載到內存中的文件,是:按行從一個流中讀取它行,尋找該名稱剛剛更改,如果匹配則跳過它,否則將該行寫回到出站流,然後追加更改的記錄。有**有更有效的方法,因爲你必須讀取它才能獲得記錄進行編輯。而且,與舊式陣列相比,存在更多更好的存儲機制,但這可能是該任務的一部分。 – Plutonix