Back to Devexpress

WriteProtectionOptions.CheckPassword(String) Method

officefileapi-devexpress-dot-spreadsheet-dot-writeprotectionoptions-dot-checkpassword-x28-system-dot-string-x29.md

latest2.7 KB
Original Source

WriteProtectionOptions.CheckPassword(String) Method

Checks whether the specified password is the same as the password used to write-protect a workbook.

Namespace : DevExpress.Spreadsheet

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

NuGet Package : DevExpress.Spreadsheet.Core

Declaration

csharp
bool CheckPassword(
    string password
)
vb
Function CheckPassword(
    password As String
) As Boolean

Parameters

NameTypeDescription
passwordString

The password to check.

|

Returns

TypeDescription
Boolean

true if the password is valid; otherwise, false.

|

Remarks

The following example checks a given password and removes write-protection from a workbook if the password is valid:

csharp
using (Workbook workbook = new Workbook()) {
    workbook.LoadDocument("WriteProtectedDocument.xlsx");
    RemoveWriteProtection(workbook, "password");
}
// ...

private void RemoveWriteProtection(Workbook workbook, string password) {
    var wpOptions = workbook.DocumentSettings.WriteProtection;
    if (wpOptions.IsPasswordProtected && wpOptions.CheckPassword(password))
        wpOptions.ClearPassword();
    else {
        Console.WriteLine("The file is not write-protected or the specified password is invalid!");
        Console.ReadKey();
    }
}
vb
Using workbook As New Workbook()
    workbook.LoadDocument("WriteProtectedDocument.xlsx")
    RemoveWriteProtection(workbook, "password")
End Using
' ...

Private Sub RemoveWriteProtection(workbook As Workbook, password As String)
    Dim wpOptions As WriteProtectionOptions = workbook.DocumentSettings.WriteProtection
    If wpOptions.IsPasswordProtected AndAlso wpOptions.CheckPassword(password) Then
        wpOptions.ClearPassword()
    Else
        Console.WriteLine("The file is not write-protected or the specified password is invalid!")
        Console.ReadKey()
    End If
End Sub

See Also

WriteProtectionOptions Interface

WriteProtectionOptions Members

DevExpress.Spreadsheet Namespace