RESOLVED: Permission error for deleting files

kasteel

Well-known member
Joined
May 29, 2009
Messages
50
Programming Experience
10+
Hi

I am concatinating a couple of text files and then deleting the individual files. (Not the output file)

The only problem is that I am getting a permission error "Exception from HRESULT: 0x800A0046 (CTL_E_PERMISSIONDENIED)" when I try to delete the files.

I can delete the files using the code below if I do not concatinate the files but as soon as I do I cannot delete them.

Any help would be appreciated. :)

To concatinate:
VB.NET:
 Public Sub MeltText()
        Dim SW As New IO.StreamWriter(OutPutFile)
        Dim Files As String() = IO.Directory.GetFiles(StartDirectory, "*.txt")
        Dim File As String

        SW.WriteLine("SwipeCard, FirstName, LastName, Faculty, Degree, IDNr, EndDate, e-mail")
        For Each File In Files
            Dim SR As New IO.StreamReader(File)
            Do Until SR.EndOfStream
                Dim TempString As String = SR.ReadLine
                SW.WriteLine(TempString)
            Loop
        Next
        SW.Flush()
        SW.Close()
        Files = Nothing
        File = Nothing

    End Sub

To Delete:
VB.NET:
 Public Sub Cleanup()
        'Delete individual txt files
        Dim CheckFileCount2 As String

        CheckFileCount2 = System.IO.Directory.GetFiles("C:\Folder1\Folder2", "*.*", IO.SearchOption.AllDirectories).Length
        If CheckFileCount2 = 0 Then
            MsgBox("Folder Empty")
            Return
        Else
            Dim obj
            obj = CreateObject("Scripting.FileSystemObject")
            obj.DeleteFile("C:\Folder1\Folder2\*.txt")
        End If
    End Sub
 
Last edited:
You're opening a StreamReader for each file, but don't close any of them. Use the Close method when you're done with the StreamReader object.
 
Back
Top