none
The process cannot access the file ... because it is being used by another process - Sometimes!

    Question

  • I am currently making a program in Visual Studio 2010 (VB.Net), and am having an issue deleting a file, using the "Kill" command. 

    Sometimes, it works perfectly fine, but other times it throws back an IOException error, and there doesn't seem to be any difference between when it works and when it doesn't - it is just random! Does anyone know of any reasons that this may be? Thanks in advance. 

    Private Sub btnUpdate_Click(sender As System.Object, e As System.EventArgs) Handles btnUpdate.Click
            Dim bookingReader As New StreamReader(Application.StartupPath & "\bookings.txt")
            Dim bookingReaderLine As String = ""
            Dim i As Integer = 0
            Dim found As Boolean = False
            Do
                bookingReaderLine = bookingReader.ReadLine
                If bookingReaderLine <> GlobalVars.strDate & ";" & GlobalVars.strTime & ";" & GlobalVars.strKart & ";" & GlobalVars.strEmail Then
                    i += 1
                Else
                    found = True
                End If
            Loop Until found = True
            bookingReader.Close()
            Dim fileLines As List(Of String) = File.ReadAllLines(Application.StartupPath & "\bookings.txt").ToList
            fileLines.RemoveAt(i)
            Kill(Application.StartupPath & "\bookings.txt")   'This line sometimes produces IOException error.
            File.WriteAllLines(Application.StartupPath & "\bookings.txt", fileLines)
    End Sub


    Sunday, March 19, 2017 7:40 PM

Answers

  • Thanks all for your feedback. In the end, the way I managed to resolve this issue was by putting it in to a try statement within a separate function. If it occurred that the file was supposedly still being used when the Kill command was closed, the 'Catch' statement would then be to recall the function to try it again (so it will keep recurring until it does it). I have no idea why it does this sometimes, but other times it doesn't, but this method seems to work - it can just be a bit variable as to how many recursions are required before it works. 
    • Marked as answer by M477H3W Wednesday, March 29, 2017 3:32 PM
    Wednesday, March 29, 2017 12:33 PM

All replies

  • Sorry, I should have said - this code is used to delete a record from a text file. 
    Sunday, March 19, 2017 7:41 PM
  • Hi

    Here is some code that may be of some help. As I can't set up an environment the same as yours, this is an attempt to illustrate one way to do it.

    NOTE: this example adds duplicate entries to show that all matches will be removed.

    Option Strict On
    Option Explicit On
    Option Infer Off
    Public Class Form1
        ' this is only a path to enable this example
        Dim testfilepath As String = My.Computer.FileSystem.SpecialDirectories.Desktop & "\TestFolder\bookings.txt"
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
            ' set up a test file with dummy data
            ' this is only for this example code
            '========================================
            Dim testdate As DateTime = Now
            Dim testdata As New List(Of TestVars)
            For i As Integer = 1 To 10
                Dim test As New TestVars
                test.strDate = testdate.ToString("dd MMM yyyy")
                test.strTim = testdate.ToString("HH:mm")
                test.strEmail = "freddy" & (i + 9).ToString & "@ihopenot.com"
                test.strKart = "Kart" & i.ToString
                testdata.Add(test)
            Next
    
            Dim testlist As New List(Of String)
            For Each t As TestVars In testdata
                Dim s As String = Nothing
                With t
                    s = .strDate & ","
                    s &= .strTim & ","
                    s &= .strEmail & ","
                    s &= .strKart
                End With
                testlist.Add(s)
                ' add twice to show multiple entries will be removed
                testlist.Add(s)
            Next
    
            IO.File.WriteAllLines(testfilepath, testlist)
    
    
            ' set up a known search for example
            Dim GlobalVars As New TestVars With {.strDate = testdate.ToString("dd MMM yyyy"), .strTim = testdate.ToString("HH:mm"), .strEmail = "freddy14@ihopenot.com", .strKart = "Kart5"}
            '========================================
    
    
            Dim fileLines As List(Of String) = IO.File.ReadAllLines(testfilepath).ToList
            Dim index As Integer = 0
            For i As Integer = fileLines.Count - 1 To 0 Step -1
                Dim f As String = fileLines(i)
                Dim sp() As String = Split(f, ",")
                Dim match As Integer = 0
                If sp(0) = GlobalVars.strDate Then match += 1
                If sp(1) = GlobalVars.strTim Then match += 1
                If sp(2) = GlobalVars.strEmail Then match += 1
                If sp(3) = GlobalVars.strKart Then match += 1
                If match = 4 Then
                    ' found match
                    fileLines.Remove(f)
                End If
            Next
            IO.File.WriteAllLines(testfilepath, fileLines)
        End Sub
        Class TestVars
            Property strDate As String
            Property strTim As String
            Property strKart As String
            Property strEmail As String
        End Class
    End Class


    Regards Les, Livingston, Scotland

    Sunday, March 19, 2017 8:50 PM
  • Thanks for your help. However, the issue I am having is not with being able to delete a record from a file - this works fine using my code. However, it is just with this specific example that it seems to state it cannot open the file because it is being used by another process:

    Dim fileLines As List(Of String) = File.ReadAllLines(Application.StartupPath & "\bookings.txt").ToList
            fileLines.RemoveAt(i)
            Kill(Application.StartupPath & "\bookings.txt")

    The thing that I find particularly odd is that the top line here runs perfectly every time. This means that before when File.ReadAllLines of the file is run, the text file cannot be open, or it would produce this IO exception error then (if I am right?). It is just when it runs the "Kill" line that the IOException error sometimes occurs, and there is no pattern as to when it does or doesn't work. 

    So this may suggest that some of the time, the File.ReadAllLines is not closing the access to the file immediately after the line has been run, if my thinking is correct? If so, anyone have any ideas as to why this is?

    Sunday, March 19, 2017 9:01 PM
  • Hi

    OK.


    Regards Les, Livingston, Scotland

    Sunday, March 19, 2017 9:30 PM
  • Matthew,

    Your reader needs to be closed/disposed or it will have the text file open.

        Private Sub ReadTextFile(ByVal filePath As String, _
                                 ByVal deleteWhenComplete As Boolean)

            If Not String.IsNullOrWhiteSpace(filePath) Then
                Dim fi As New IO.FileInfo(filePath)

                If fi.Exists Then
                    Using rdr As New System.IO.StreamReader(fi.FullName)
                        Do While rdr.Peek() >= 0
                            Dim itm As String = rdr.ReadLine.Trim
                            'Now process the line “itm”
                        Loop
                    End Using

                    If deleteWhenComplete Then
                        fi.Delete()
                    End If
                End If
            End If

        End Sub

    Try that instead and let's see?


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Sunday, March 19, 2017 9:39 PM
  • So this may suggest that some of the time, the File.ReadAllLines is not closing the access to the file immediately after the line has been run, if my thinking is correct? If so, anyone have any ideas as to why this is?

    Removing the filename from the list is not associated with closing the file. ReadAllLines is not responsible for making the file available - all it does it tell the OS that it no longer needs that file.  The OS is responsible for managing whether or not the file is actually available to be deleted.

    You should write the modified lines to a temporary file, then delete the original file, and rename the temporary filename to the original filename.

    Sunday, March 19, 2017 9:42 PM

  • Dim fileLines As List(Of String) = File.ReadAllLines(Application.StartupPath & "\bookings.txt").ToList
            fileLines.RemoveAt(i)
            Kill(Application.StartupPath & "\bookings.txt")


    File.ReadAllLines?

    You changed it from what you originally posted - that's confusing.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Sunday, March 19, 2017 9:49 PM
  • No I didn't - it says File.ReadAllLines in the initial post
    Sunday, March 19, 2017 9:52 PM
  • Thanks all for your help. I may have managed to get around this problem by inserting the kill function in to a 'Try' statement inside a 'While' loop. This way, i have it keep on looping until the IOException catch does not occur. Not sure if this will continue to work very efficiently but has worked fine for me so far, so will see how it goes & will try some of your suggestions if this is flawed. 
    Sunday, March 19, 2017 9:54 PM
  • No I didn't - it says File.ReadAllLines in the initial post

    Assuming you're talking to me:

    Dim bookingReader As New StreamReader(Application.StartupPath & "\bookings.txt")\


    What am I missing?

    *****

    If you want, explain from the start just what you're trying to do and we'll go from there.

    Also, you may not be able to delete the file in the path that you show  at least not without elevating privileges.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Sunday, March 19, 2017 10:00 PM
  • Frank, Line 15 of the code block on the initial post is what I've been referring to. 
    Sunday, March 19, 2017 10:03 PM
  • Frank, Line 15 of the code block on the initial post is what I've been referring to. 

    Why are you using Kill and not File.Delete (or similar like I showed with FileInfo)?

    If you'll explain in plain words - not code - what you're trying to do then I feel sure a better solution can be had.

    I'll add this too: For a file path, use System.IO.Path - let it work out the path as that's what it was designed to do.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Sunday, March 19, 2017 10:10 PM
  • Frank, Line 15 of the code block on the initial post is what I've been referring to. 

    This:

    Loop Until found = True

    Is very dangerous. It may never be found - runaway train!


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Sunday, March 19, 2017 10:11 PM
  • Frank, Line 15 of the code block on the initial post is what I've been referring to. 

    Lastly ... if you open the file with a StreamReader -- it's open. Close and dispose or better yet, put it in a Using block which will do it for you, even if an exception is thrown (it uses the Finally).

    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Sunday, March 19, 2017 10:12 PM
  • Hi M477H3W,

    It seems that you have solved your issue by yourself, please click "Mark as answer" to close your post. If you have another questions, please create new thread.

    Thanks for you understanding.

    Best Regards,

    Cherry Bu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, March 20, 2017 1:47 AM
    Moderator
  • Thanks all for your feedback. In the end, the way I managed to resolve this issue was by putting it in to a try statement within a separate function. If it occurred that the file was supposedly still being used when the Kill command was closed, the 'Catch' statement would then be to recall the function to try it again (so it will keep recurring until it does it). I have no idea why it does this sometimes, but other times it doesn't, but this method seems to work - it can just be a bit variable as to how many recursions are required before it works. 
    • Marked as answer by M477H3W Wednesday, March 29, 2017 3:32 PM
    Wednesday, March 29, 2017 12:33 PM