Back to Devexpress

RichEditDocumentServer.EncryptedFilePasswordCheckFailed Event

officefileapi-devexpress-dot-xtrarichedit-dot-richeditdocumentserver-68bc85f9.md

latest8.8 KB
Original Source

RichEditDocumentServer.EncryptedFilePasswordCheckFailed Event

Occurs when the encryption password is empty or invalid.

Namespace : DevExpress.XtraRichEdit

Assembly : DevExpress.RichEdit.v25.2.Core.dll

NuGet Package : DevExpress.RichEdit.Core

Declaration

csharp
public event EncryptedFilePasswordCheckFailedEventHandler EncryptedFilePasswordCheckFailed
vb
Public Event EncryptedFilePasswordCheckFailed As EncryptedFilePasswordCheckFailedEventHandler

Event Data

The EncryptedFilePasswordCheckFailed event's data class is EncryptedFilePasswordCheckFailedEventArgs. The following properties provide information specific to this event:

PropertyDescription
DocumentNameGets the encrypted document’s name.
ErrorObtains the error type caused the event to raise.
HandledGets or sets a value that indicates whether the event handler has completely handled the event or whether the system should continue its own processing. Inherited from HandledEventArgs.
TryAgainGets or sets whether to prompt with a password again.

Remarks

Hanlde the EncryptedFilePasswordCheckFailed event to determine the password error (RichEditDecryptionError.PasswordRequired or RichEditDecryptionError.WrongPassword, and specify whether to enable a new password request (TryAgain).

If the TryAgain property is set to true , the RichEditDocumentServer.EncryptedFilePasswordRequested event occurs. The RichEditDocumentServer.InvalidFormatException event fires when the TryAgain property is not specifies or set to false.

The code sample below shows how to handle the RichEditDocumentServer.EncryptedFilePasswordRequested and RichEditDocumentServer.EncryptedFilePasswordCheckFailed events to prompt users to enter a password. If the user cancels the operation, the RichEditDocumentServer loads an empty file.

View Example

csharp
private static void Server_EncryptedFilePasswordRequested(object sender, EncryptedFilePasswordRequestedEventArgs e)
{
    //Prompt the user to enter the password
    Console.WriteLine("Enter password:");
    e.Password = Console.ReadLine();
    e.Handled = true;
}

private static void Server_EncryptedFilePasswordCheckFailed(object sender, EncryptedFilePasswordCheckFailedEventArgs e)
{
    //Analyze the password error:
    switch (e.Error)
    {
        //If the password is empty, raise the request again
        case RichEditDecryptionError.PasswordRequired:
            Console.WriteLine("You did not enter the password!");
            e.TryAgain = true;
            e.Handled = true;
        break;
        //If the password is invalid, ask user whether to continue the operation:
        case RichEditDecryptionError.WrongPassword:
            Console.WriteLine("The password is incorrect. Try Again? (yes/no)");
            string answer = Console.ReadLine()?.ToLower();
            if (answer == "y")
            {
                e.TryAgain = true;
                e.Handled = true;
            }
            //If user cancels the operation, open an empty file:
            else
            {
                Console.WriteLine("Password check failed. Loading an empty file...");
            }
        break;
    }
}

private static void Server_InvalidFormatException(object sender, RichEditInvalidFormatExceptionEventArgs e)
{
    RichEditDocumentServer server = (RichEditDocumentServer)sender;

    //Save and open the resulting file
    server.SaveDocument("EmptyFile.docx", DocumentFormat.Docx);
    Process.Start("EmptyFile.docx");
}
vb
Private Shared Sub Server_EncryptedFilePasswordRequested(ByVal sender As Object, ByVal e As EncryptedFilePasswordRequestedEventArgs)
    'Prompt the user to enter the password
    Console.WriteLine("Enter password:")
    e.Password = Console.ReadLine()
    e.Handled = True
End Sub

Private Shared Sub Server_EncryptedFilePasswordCheckFailed(ByVal sender As Object, ByVal e As EncryptedFilePasswordCheckFailedEventArgs)

    'Analyze the password error:
    Select Case e.[Error]
        'If the password is empty, raise the request again
        Case RichEditDecryptionError.PasswordRequired
            Console.WriteLine("You did not enter the password!")
            e.TryAgain = True
            e.Handled = True
        'If the password is invalid, ask user whether to continue the operation:
        Case RichEditDecryptionError.WrongPassword
            Console.WriteLine("The password is incorrect. Try Again? (yes/no)")
            Dim answer As String = Console.ReadLine()?.ToLower()

            If answer = "y" Then
                e.TryAgain = True
                e.Handled = True
            'If user cancels the operation, open an empty file:
            Else
                Console.WriteLine("Password check failed. Loading an empty file...")
            End If
    End Select
End Sub

Private Shared Sub Server_InvalidFormatException(ByVal sender As Object, ByVal e As RichEditInvalidFormatExceptionEventArgs)
    'Save and open the resulting file
    Dim server As RichEditDocumentServer = CType(sender, RichEditDocumentServer)
    server.SaveDocument("EmptyFile.docx", DocumentFormat.Docx)
    Process.Start("EmptyFile.docx")
End Sub

The following code snippet (auto-collected from DevExpress Examples) contains a reference to the EncryptedFilePasswordCheckFailed event.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

word-document-api-open-and-save-encrypted-files/CS/Program.cs#L20

csharp
server.EncryptedFilePasswordRequested += Server_EncryptedFilePasswordRequested;
server.EncryptedFilePasswordCheckFailed += Server_EncryptedFilePasswordCheckFailed;
server.DecryptionFailed += Server_DecryptionFailed;

word-document-api-open-and-save-encrypted-files/VB/Program.vb#L15

vb
AddHandler server.EncryptedFilePasswordRequested, AddressOf Server_EncryptedFilePasswordRequested
AddHandler server.EncryptedFilePasswordCheckFailed, AddressOf Server_EncryptedFilePasswordCheckFailed
AddHandler server.DecryptionFailed, AddressOf Server_DecryptionFailed

Implements

EncryptedFilePasswordCheckFailed

See Also

RichEditDocumentServer Class

RichEditDocumentServer Members

DevExpress.XtraRichEdit Namespace