Skip to main content
Row

Workbook.EncryptedFilePasswordRequest Event

Raises when the WorkbookImportOptions.Password property is not set or contains a wrong password.

You need a license for the DevExpress Office File API Subscription or DevExpress Universal Subscription to use this event in production code.

Namespace: DevExpress.Spreadsheet

Assembly: DevExpress.Docs.v24.1.dll

NuGet Package: DevExpress.Document.Processor

Declaration

public event EncryptedFilePasswordRequestEventHandler EncryptedFilePasswordRequest

Event Data

The EncryptedFilePasswordRequest event's data class is EncryptedFilePasswordRequestEventArgs. 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 EncryptedFilePasswordRequest event allows to specify a password in code using the EncryptedFilePasswordRequestEventArgs.Password property.

The code sample below shows how to handle the Workbook.EncryptedFilePasswordRequest and Workbook.EncryptedFilePasswordCheckFailed events to prompt users to enter a password. If the user cancels the operation, the Spreadsheet Document API shows an exception message and cancels loading the file.

View Example

static bool IsValid { get; set; }

static void Main(string[] args)
{
    Workbook workbook = new Workbook();

    workbook.EncryptedFilePasswordRequest += Workbook_EncryptedFilePasswordRequest;
    workbook.EncryptedFilePasswordCheckFailed += Workbook_EncryptedFilePasswordCheckFailed;
    workbook.InvalidFormatException += Workbook_InvalidFormatException;

    workbook.LoadDocument("Documents\\encrypted.xlsx");
}

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

private static void Workbook_EncryptedFilePasswordCheckFailed(object sender, EncryptedFilePasswordCheckFailedEventArgs e)
{
    //Analyze the password error:
    switch (e.Error)
    {

        //If the password is empty, raise the request again
        case SpreadsheetDecryptionError.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 SpreadsheetDecryptionError.WrongPassword:
            Console.WriteLine("The password is incorrect. Try Again? (y/n)");
            string answer = Console.ReadLine()?.ToLower();
            if (answer == "y")
            {
                e.TryAgain = true;
                e.Handled = true;
            }

            //If user cancels the operation, show an exception message:
            else
            {
                IsValid = false;
            }
            break;
    }

    Program.IsValid = false;
}

    private static void Workbook_InvalidFormatException(object sender, SpreadsheetInvalidFormatExceptionEventArgs e)
{
    Console.WriteLine(e.Exception.Message.ToString() + " Press any key to close...");
    Console.ReadKey(true);
}

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