Results 1 to 5 of 5

Thread: [RESOLVED] the process cannot access the file because it is being used by another process

  1. #1

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,204

    Resolved [RESOLVED] the process cannot access the file because it is being used by another process

    im using StreamReader to read some content from a textfile
    after im reading i want to delete it.
    but i get this error
    the process cannot access the file because it is being used by another process
    i know the reader needs to be closed and its self closing
    but still error pop ups when tried to delete the file.

    btw i know that end using needs to close the reader right?
    Code:
    Private Sub Readfile()
            Dim line As String = String.Empty
    
            Try
    
                Using objReader As New StreamReader("C:\ESystem\EOUT\ECharge.out")
                    line = objReader.ReadLine
                    If Mid(line, 1, 3) = "000" Then
                        Dim EResultRecord As String = line
                        DialogResult = DialogResult.OK
                    Else
                        EResultRecord = 300
                    End If
                End Using
    
             'delete file after reading          
             Delete_Out_EFiles()
    
            Catch ex As Exception
                MessageBox.Show(ex.Message, "err", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        End Sub
    Code:
        Public Shared Sub Delete_Out_EFiles()
    
            Try
    
                Dim pathToDir As String = "C:\ESystem\EOUT"
                Dim myDir As DirectoryInfo = New DirectoryInfo(pathToDir)
                If myDir.EnumerateFiles().Any() Then
                    Kill("C:\ESystem\EOUT\*.*")
                End If
    
            Catch ex As Exception
                MessageBox.Show(ex.Message, "err", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        End Sub
    tnx for any help
    salsa 31
    Last edited by salsa31; Sep 18th, 2021 at 06:49 AM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: the process cannot access the file because it is being used by another process

    You are aware that the path you're passing to Kill is different from that path of the file that you're opening with the StreamReader, right?

  3. #3

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,204

    Re: the process cannot access the file because it is being used by another process

    sry my bad
    fixed it2
    any way why do i get this error?

  4. #4
    Addicted Member
    Join Date
    Jul 2017
    Location
    Exeter, UK
    Posts
    180

    Re: the process cannot access the file because it is being used by another process

    Two points you may consider relevant.

    1. The documentation https://docs.microsoft.com/en-us/dot...l?view=net-5.0 states in the Remarks section, Security Note In order to execute, the Kill function requires Read and PathDiscovery flags of FileIOPermission to be granted to the executing code. For more information, see SecurityExceptionCode Access Permissions.

    2. Have you tried moving the call to Delete_Out_EFiles() out of the Try/catch block?

  5. #5

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,204

    Re: the process cannot access the file because it is being used by another process

    1. The documentation https://docs.microsoft.com/en-us/dot...l?view=net-5.0 states in the Remarks section, Security Note In order to execute, the Kill function requires Read and PathDiscovery flags of FileIOPermission to be granted to the executing code. For more information, see SecurityExceptionCode Access Permissions.
    the file has everyone full permissions

    2. Have you tried moving the call to Delete_Out_EFiles() out of the Try/catch block?
    will this fix the issue?
    the kill process is working ok
    problem with streamreader i think.

    is it realy closing the file?
    Code:
        Dim line As String = String.Empty
    
            Try
    
                Using objReader As New StreamReader("C:\ESystem\EOUT\ECharge.out")
                    line = objReader.ReadLine
                    If Mid(line, 1, 3) = "000" Then
                        Dim EResultRecord As String = line
                        DialogResult = DialogResult.OK
                    Else
                        EResultRecord = 300
                    End If
                End Using
    
             'delete file after reading          
             Delete_Out_EFiles()
    
            Catch ex As Exception
                MessageBox.Show(ex.Message, "err", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width