0

I want to make a program that can read and delete text file in drive C: I am able to read already but deleting is my problem. I want to auto delete the text file after entering the correct username and password.

Here is my code. When i click confirm button after entering pass and username.

I tried the code below but it said an error.

The process cannot access the file ..("C:\Users\smt32\Documents\CP1 FILES\buzzer\test.txt"). because it is being used by another process

Private Sub btnConfirm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConfirm.Click
    Dim user, pass As String
    user = (txtUserName.Text)
    pass = (txtPass.Text)

    If user = "test" And pass = "test" Then
        Me.Close()
        'Kill("C:\Users\smt32\Documents\CP1 FILES\buzzer\test.txt")
        ' My.Computer.FileSystem.DeleteFile("C:\Users\smt32\Documents\CP1 FILES\buzzer\test.txt")
        System.IO.File.Delete("C:\Users\smt32\Documents\CP1 FILES\buzzer\test.txt")
    Else
        MsgBox("wrong pass")


    End If
End Sub
braX
  • 11,506
  • 5
  • 20
  • 33
Bonito
  • 77
  • 10
  • 4
    As the error says, it's open somewhere. Is your program opening it and not closing? – Sami Kuhmonen Feb 13 '18 at 13:11
  • 4
    Maybe you aren't properly closing your file after reading it. – the_lotus Feb 13 '18 at 13:12
  • 1
    Possible duplicate of [IOException: The process cannot access the file 'file path' because it is being used by another process](https://stackoverflow.com/questions/26741191/ioexception-the-process-cannot-access-the-file-file-path-because-it-is-being) – L-Four Feb 13 '18 at 14:51
  • @the_lotus you are correct i didn't close it after reading. thanks for that ..suggestion – Bonito Feb 13 '18 at 15:41
  • thank you all for the suggestion. I salute you guys! – Bonito Feb 13 '18 at 15:42

2 Answers2

0

I close my txtreader after reading.

         Public Sub checkTxt()
    Try
    Dim txtReader As New System.IO.StreamReader("C:\Users\smt32\Documents\CP1 FILES\buzzer\test.txt")
    txtAlert.Text = txtReader.ReadToEnd
    If txtAlert.Text <> "" Then
        playSound()
        Timer1.Stop() 'stop the counter if found data
            AlertForm.Show()
            txtReader.Close()'i add this code so that i can delete
    Else
        End If
    Catch ex As Exception
        MsgBox("No file found")
    End Try

End Sub
Bonito
  • 77
  • 10
  • 2
    That's not where you should close it (what if txtAlert.Text = ""?) Put the reader in a Using...End Using block. Or just read the contents of the file with File.ReadAllLines or File.ReadAllText. – LarsTech Feb 13 '18 at 15:49
  • @LarsTech thanks for the answer please help vote my question . to increase my reputation.. thank you Sir. – Bonito Jun 12 '18 at 13:53
0

If you can use File.Delete if you have not opened the file for reading, which you should test, then it is your code which has an open handle to it.

To avoid that as easily as possible, use the Using..End Using block, as has been suggested.

        Dim fullText As String
        Using r As New System.IO.StreamReader(FileFullPath)
            fullText = r.ReadToEnd
        End Using
        ' do something with the full text; the StreamReader has now closed gracefully
El-Ahrairah
  • 153
  • 1
  • 7