Skip to main content
Row

EncryptedFilePasswordCheckFailedEventArgs Class

Provides data for the EncryptedFilePasswordCheckFailed event.

Namespace: DevExpress.Spreadsheet

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

NuGet Package: DevExpress.Spreadsheet.Core

Declaration

public class EncryptedFilePasswordCheckFailedEventArgs :
    HandledEventArgs

Remarks

The following code snippet handles 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 the file loading operation.

View Example: Load and Save Password-Encrypted Files

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);
}

Inheritance

Object
EventArgs
HandledEventArgs
EncryptedFilePasswordCheckFailedEventArgs
See Also