Function Reference


_WinAPI_EncryptFile

Encrypts a file or directory

#include <WinAPIFiles.au3>
_WinAPI_EncryptFile ( $sFilePath )

Parameters

$sFilePath The name of the file or directory to be encrypted.
If $sFilePath specifies a read-only file, the function fails and the last error code is ERROR_FILE_READ_ONLY (6009).
If $sFilePath specifies a directory that contains a read-only file, the functions succeeds but the directory is not encrypted.

Return Value

Success: True.
Failure: False, call _WinAPI_GetLastError() to get extended error information

Remarks

The _WinAPI_EncryptFile() function requires exclusive access to the file being encrypted, and will fail if another process is using the file.

If the file is already encrypted, the function simply returns a nonzero value, which indicates success.
If the file is compressed, the function will decompress the file before encrypting it.

To decrypt an encrypted file, use the _WinAPI_DecryptFile() function.

Related

_WinAPI_DecryptFile

See Also

Search EncryptFile in MSDN Library.

Example

#include <APIFilesConstants.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIFiles.au3>

Local $sFile = FileOpenDialog('Select File', @ScriptDir, 'All Files (*.*)', BitOR($FD_FILEMUSTEXIST, $FD_PATHMUSTEXIST))
If @error Then Exit

Switch _WinAPI_FileEncryptionStatus($sFile)
        Case $FILE_ENCRYPTABLE
                If _WinAPI_EncryptFile($sFile) Then
                        MsgBox(($MB_ICONINFORMATION + $MB_SYSTEMMODAL), 'Encryption File', 'The file encrypted is successfully.')
                Else
                        MsgBox(($MB_ICONERROR + $MB_SYSTEMMODAL), 'Encryption File', 'Unable to encrypt file.')
                EndIf
        Case $FILE_IS_ENCRYPTED
                If MsgBox(($MB_YESNO + $MB_ICONQUESTION + $MB_SYSTEMMODAL), 'Encryption File', 'The file is already encrypted.' & @CRLF & @CRLF & 'Decrypt?') = 6 Then
                        If _WinAPI_DecryptFile($sFile) Then
                                MsgBox(($MB_ICONINFORMATION + $MB_SYSTEMMODAL), 'Encryption File', 'The file decrypted is successfully.')
                        Else
                                MsgBox(($MB_ICONERROR + $MB_SYSTEMMODAL), 'Encryption File', 'Unable to decrypt file.')
                        EndIf
                EndIf
        Case Else
                MsgBox(($MB_ICONERROR + $MB_SYSTEMMODAL), 'Encryption File', 'Unable to perform operation.')
EndSwitch