-
Sep 18th, 2021, 06:41 AM
#1
Thread Starter
Enjoy the moment
[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.
-
Sep 18th, 2021, 06:46 AM
#2
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?
-
Sep 18th, 2021, 06:50 AM
#3
Thread Starter
Enjoy the moment
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?
-
Sep 18th, 2021, 08:30 AM
#4
Addicted Member
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?
-
Sep 18th, 2021, 08:36 AM
#5
Thread Starter
Enjoy the moment
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|