VB.NET - File permission denied for delete - Asked By Jerry Lafargue on 16-Apr-07 03:16 PM

I'm writing a little VB.Net application that processes a directory of text files.  For each text file it finds, it copies it to a backup directory, processes the file, and then deletes the file.  When I try to delete the file, I get this error message:

Exception from HRESULT: 0x800a0046(CTL_E_PERMISSIONDENIED)

Here's the code (I boldfaced the delete command) : 

Private Sub Process_file(ByRef strFilename As String)

' The data base connection string

Dim strConnection As String = "Provider=MICROSOFT.JET.OLEDB.4.0;" _

& "DATA SOURCE=\\pdis2k18\apps\data\ftp\DispositionReports\MSAccessDatabase\DispositionStatistics.mdb;" _

& "Mode=|Share Deny None|;"

' The data base connection object

Dim objConnection As New System.Data.OleDb.OleDbConnection(strConnection)

' Insert command

Dim oleDbInsert As New OleDb.OleDbCommand()

' Set up the connection for the insert command

oleDbInsert.Connection = objConnection

oleDbInsert.CommandText = _

"Insert into DispositionStats " _

& " (Warrant, WarrantType, Action, ErrorMessage, DispositionDate, " _

& " VALUES ( " & "'" & "strWno" & "'," _

& "'" & "strWtyp" & "'," _

& "'" & "strAction" & "'," _

& "'" & "strErrMsg" & "'," _

& "'" & "dtDispDate" & "')"

' This is used to read in the text of the file

Dim txtTextStream As Scripting.TextStream

' This is used for one line/record of the file

Dim strOneLine As String

' Fields in one record. Add them to the insert command as parameters

' as they are declared.

Dim strWno As String

Dim strWtyp As String

Dim strAction As String

Dim strErrMsg As String

Dim dtDispDate As Date = FileDateTime(strFilename)

' End of file flag

Dim strEOF = "N"

' Read in the entire text

Try

objConnection.Open()

txtTextStream = Fsys.GetFile(strFilename).OpenAsTextStream(Scripting.IOMode.ForReading)

strOneLine = txtTextStream.ReadLine

If strOneLine = "" Then

strEOF = "Y"

Else

strWno = Mid(strOneLine, 1, 12)

strWtyp = Mid(strOneLine, 14, 2)

strAction = Mid(strOneLine, 16, 1)

strErrMsg = Mid(strOneLine, 18, strOneLine.Length - 15)

' insert the row

End If

' open the connection

Do Until txtTextStream.AtEndOfStream

strOneLine = txtTextStream.ReadLine

If strOneLine = "" Then

strEOF = "Y"

Else

strWno = Mid(strOneLine, 1, 12)

strWtyp = Mid(strOneLine, 14, 2)

strAction = Mid(strOneLine, 16, 1)

strErrMsg = Mid(strOneLine, 18, strOneLine.Length - 15)

' insert the row

End If

Loop

' close the connection

objConnection.Close()

Fsys.GetFile(strFilename).Delete()

Catch e As System.Exception

MessageBox.Show(e.Message.ToString)

End Try

End Sub



Couple Items - Peter Bromberg replied to Jerry Lafargue on 16-Apr-07 04:00 PM

It looks like you are using COM FileSystemObject Scripting.TextStream object, why would you want to do this when the .NET Framework has a complete managed code set of file and stream classes?

It looks to me like you never closed the original TextStream when you go to delete the file. Take another look at the methods, or better yet, switch to pure .NET. You can't easily delete a file that is currently open and in use.


Using a COM FileSystemObject - Jerry Lafargue replied to Peter Bromberg on 16-Apr-07 05:15 PM

Oops.  Forgot the close.  It worked!  Thanks Pete!

The reason I'm using a COM FileSystemObject is because that is a technique with which I am reasonably familiar (except for temporary brain dysfunction forgetting to close the file) and my bosses want this as soon as possible, i.e. last week.  I'm still new to DotNet.