2016-06-18 77 views
-3

我正在嘗試構建預訂和付款系統,目前我有多個變量保存到文件中。我需要做的是將付款添加到設置的預訂ID,現在我已經對它進行了排序,因此它將更改的付款總額添加到文件中,但我不能讓它刪除舊的。因此,當它搜索文件時,它會先找到舊的,而客戶只需支付50英鎊,而不是最新支付100英鎊的那個。那麼,我如何從文件中讀取數據,然後完成所需的所有數據處理,然後刪除單行數據,然後我知道如何添加新數據。使用數組的朋友提到過,但我試過了在這裏仔細觀察,這讓我大失所望。從vb.net的文本文件中刪除一行

在此先感謝

上面有它找到的數據更多的代碼,並顯示它,那麼這是,一旦他們輸入自己想要加的

Dim bookings As String 
    bookings = "E:\Grouse Lodge Bookings\Bookings.txt" 
    bookingidtemp = txtbookingid.Text 
    FileOpen(1, bookings, OpenMode.Input) 
    found = False 
    If temp = True Then 
     If found = False Then 
      Do Until found = True 
       Input(1, custname) 
       Input(1, address1) 
       Input(1, address2) 
       Input(1, cost) 
       Input(1, amount) 
       Input(1, startdate) 
       Input(1, enddate) 
       Input(1, postcode) 
       Input(1, bookingid) 
       Input(1, remaining) 
       If bookingidtemp = bookingid Then 
        txtname.Text = custname 
        txtaddress1.Text = address1 
        txtaddress2.Text = address2 
        txtcost.Text = cost 
        txtstartdate.Text = startdate 
        txtenddate.Text = enddate 
        txtpostcode.Text = postcode 
        txttopay.Text = remaining 
        found = True 
        FileClose(1) 
       Else 

       End If 
      Loop 
     End If 
     If found = False Then 
      MsgBox("Booking not found, please try again") 
      FileClose(1) 
     Else 
      If found = True Then 
       FileOpen(1, bookings, OpenMode.append) 
       custname = txtname.Text 
       address1 = txtaddress1.Text 
       address2 = txtaddress2.Text 
       cost = txtcost.Text 
       amount = txtpaid.Text 
       startdate = txtstartdate.Text 
       enddate = txtenddate.Text 
       postcode = txtpostcode.Text 
       bookingid = txtbookingid.Text 
       cost = txtcost.Text 
       amount = txtpaid.Text 
       paying = remaining - amount 
       remaining = paying 
       amount = cost - remaining 
       WriteLine(1, custname, address1, address2, cost, amount, startdate, enddate, postcode, bookingid, remaining) 
       FileClose(1) 
       MsgBox("Payment added") 
+3

文本文件不是隨機訪問。更改一行的唯一方法是再次寫出整個文件。考慮一個數據庫。也請閱讀[Ask]並參加[Tour] – Plutonix

+0

你有什麼試過的?我們沒有人可以幫助你,除非我有一些工作,特別是[最小,完整和可驗證示例(MCVE)](https://stackoverflow.com/help/mcve)。解釋這個問題的一個好方法是包含你迄今爲止編寫的代碼,示例輸入(如果有的話),期望的輸出以及實際獲得的輸出(控制檯輸出,回溯等)。您提供的細節越多,您可能會收到的答案就越多。另外,請查看[常見問題](http://meta.stackexchange.com/q/7931)和[問](https://stackoverflow.com/help/asking)。 –

+0

將其添加到問題中。 –

回答

0

也許量..... 。

Public Sub rml(ByVal path As String, ByVal ln As Integer) 
    Dim tb As New TextBox 
    tb.Text = My.Computer.FileSystem.ReadAllText(path) 
    Dim str As New List(Of String) 
    Dim i As Integer = 0 
    For Each l In tb.Lines 
     str.Add(tb.Lines(i)) 
     i += 1 
    Next 
    str.RemoveAt(ln - 1) 
    tb.Text = "" 
    For Each h In str 
     tb.Text = tb.Text & h & vbNewLine 
    Next 
    Dim writer As New System.IO.StreamWriter(path) 
    writer.Write(tb.Text) 
    writer.Close() 
End Sub 

實施例:rml("C:\Users\User1\Desktop\TextEx.txt", 5)將刪除該文本文件的第五行 「TextEx」