[]
        
(Showing Draft Content)

GrapeCity.Documents.Excel.IWorkbook.IsEncryptedFile

IsEncryptedFile Method

IsEncryptedFile(string)

Determines whether the specified file is password protected.

Use this method to check whether a workbook file requires a password before opening it. If the file is encrypted, open it with Open(string, string, OpenOptions).

Declaration
bool IsEncryptedFile(string fileName)
Function IsEncryptedFile(fileName As String) As Boolean
Parameters
Type Name Description
string fileName

The name or path of the file to check. Must not be null.

Returns
Type Description
bool

true if the specified file is password protected; otherwise, false.

Examples
worksheet.Range["A1"].Value = "Confidential";
string filePath = Path.Combine(Path.GetTempPath(), Path.ChangeExtension(Path.GetRandomFileName(), ".xlsx"));
string password = GetPasswordFromSecureSource();
XlsxSaveOptions options = new XlsxSaveOptions();
options.Password = password;
workbook.Save(filePath, options);
bool isEncrypted = workbook.IsEncryptedFile(filePath);
Exceptions
Type Condition
IOException

Thrown when the specified file cannot be opened for reading.

UnauthorizedAccessException

Thrown when the caller does not have permission to access the specified file.

IsEncryptedFile(Stream)

Determines whether the specified file stream is password protected.

Use this method before opening a workbook stream to decide whether a password should be supplied to Open(Stream, string, OpenOptions).

Declaration
bool IsEncryptedFile(Stream fileStream)
Function IsEncryptedFile(fileStream As Stream) As Boolean
Parameters
Type Name Description
Stream fileStream

The input file stream to inspect.

Returns
Type Description
bool

true if the specified file stream is password protected; otherwise, false.

Examples
worksheet.Range["A1"].Value = "Confidential";
using (MemoryStream fileStream = new MemoryStream())
{
    string password = GetPasswordFromSecureSource();
    XlsxSaveOptions options = new XlsxSaveOptions();
    options.Password = password;
    workbook.Save(fileStream, options);
    fileStream.Position = 0;
    bool encrypted = workbook.IsEncryptedFile(fileStream);
}