Hello all, zoidmaster here. I have a question regarding Visual Basic and deleting lines of text from files. I'm trying to make a piece of software to manage customers who walk into a store. I got most of it written already but the problem I'm running into is deleting some text from my text file. The reason I'm starting a new thread for this is because no matter where I look I can't find a thread on any site (not just daniweb) that explains how to do this in visual basic and not vb.net or vbscript.
Here's the code from the private sub I'm working in:
Private Sub btnDone_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDone.Click
Dim data As StreamWriter 'Sets the variable to house all data being written to the file
Dim fileDateTime As String = DateTime.Now.ToString("yyyyMMdd")
data = My.Computer.FileSystem.OpenTextFileWriter("C:\Customer Assistance Kiosk\CAK_FINAL\" + fileDateTime + ".txt", True) 'Opens the file to write to and ammends all incoming lines of text
If System.IO.File.Exists("C:\Customer Assistance Kiosk\CAK_FINAL\" + fileDateTime + ".txt") = True Then
data.WriteLine("Customer Name: " + lblName.Text) 'Writes line of text from the name box
data.WriteLine("Customer Reason: " + lblReason.Text) 'Writes line of text from the combo box
data.WriteLine("Employee Number: " + txtEmpNum.Text) 'Writes line of text from the text box
data.WriteLine("-------------------------------------------") 'Inserts line separator between pieces of data in the file
data.Close() 'Closes the file after writing to it
lblName.Text = "" 'Clears the text box for the next customer's name
lblReason.Text = "" 'Clears the combo box for the next customer's reason
txtEmpNum.Text = "" 'Clears the text box for the next employee's number
Me.Close()
Else
System.IO.File.Create("C:\Customer Assistance Kiosk\CAK_FINAL\" + fileDateTime + ".txt") 'Creates the file to house the information
data.WriteLine("Customer Name: " + lblName.Text) 'Writes line of text from the name box
data.WriteLine("Customer Reason: " + lblReason.Text) 'Writes line of text from the combo box
data.WriteLine("Employee Number: " + txtEmpNum.Text) 'Writes line of text from the text box
data.WriteLine("-------------------------------------------") 'Inserts line separator between pieces of data in the file
data.Close() 'Closes the file after writing to it
lblName.Text = "" 'Clears the text box for the next customer's name
lblReason.Text = "" 'Clears the combo box for the next customer's reason
txtEmpNum.Text = "" 'Clears the text box for the next employee's number
Dim lines As New List(Of String)(File.ReadAllLines("C:\Customer Assistance Kiosk\CAK_DATA\DATA.cak"))
'Remove the line to delete, e.g.
lines.RemoveAt(1)
lines.RemoveAt(2)
File.WriteAllLines("C:\Customer Assistance Kiosk\CAK_DATA\DATA.cak", lines.ToArray())
Me.Close()
End If
End Sub
Basically what I'm trying to do here is take a file that is made in another portion of the program and delete the first two lines out of it. From what I can tell everything else works, including the portion that copies the data from one file to the other. I just don't know how to get rid of those two lines in the first file.
Any help would be much appreciated. Thank you. -Zoidmaster