Skip to main content

RichEditDocumentServer.EncryptedFilePasswordRequested Event

Raises when the RichEditDocumentImportOptions.EncryptionPassword property is not set or returns an invalid password.

Namespace: DevExpress.XtraRichEdit

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

NuGet Packages: DevExpress.RichEdit.Core, DevExpress.Win.Navigation

Declaration

public event EncryptedFilePasswordRequestedEventHandler EncryptedFilePasswordRequested

Event Data

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

Property Description
Cancel Gets or sets a value indicating whether the event should be canceled. Inherited from CancelEventArgs.
DocumentName Gets the name of the encrypted document.
Password Gets or sets the password used to encrypt the document.

Remarks

The EncryptedFilePasswordRequested event allows you to specify a password in code using the Password property. If the given password is empty or invalid, the RichEditDocumentServer.EncryptedFilePasswordCheckFailed event raises.

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 or exceeds the number of attempts to enter the password, the RichEditDocumentServer loads an empty file.

View Example

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.OpenXml);
    Process.Start("EmptyFile.docx");
}

The following code snippet (auto-collected from DevExpress Examples) contains a reference to the EncryptedFilePasswordRequested 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.

See Also