How do you set the NTFS permissions for an append-only log, and append to it in VB6?

76 Views Asked by At

I'm trying to create an append-only audit log in our VB6 app that would reside on a share folder. I'm first trying to get it working on a local file and I have the NTFS permissions with "Create folders / append data" checked and "Create files / write data" unchecked, but I'm getting a permission denied error when opening the file for Append. I don't want the users to be able to modify the existing log entries.

Log NTFS permissions Log NTFS permissions

VB6 code:

Private Sub WriteTextToFile(text As String, filePath As String)
    Dim fileNumber As Integer

    ' Open the file for writing
    fileNumber = FreeFile
    Open filePath For Append As fileNumber
    
    ' Write the text to the file
    Print #fileNumber, text
    
    ' Close the file
    Close fileNumber

End Sub

I'm having the same issue in VB.NET as well:

Private Sub AppendTextToFile(text As String, filePath As String)
    Try
        ' Use StreamWriter with Append mode
        Using writer As New StreamWriter(filePath, True)
            ' Write the text to the end of the file
            writer.WriteLine(text)
        End Using

        MessageBox.Show("Text appended to file successfully!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information)
    Catch ex As Exception
        MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try
End Sub
0

There are 0 best solutions below