[]
        
(Showing Draft Content)

GrapeCity.Documents.Excel.Workbook.IsEncryptedFile

IsEncryptedFile Method

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
public bool IsEncryptedFile(Stream fileStream)
Public 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.

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

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
public bool IsEncryptedFile(string fileName)
Public 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.

Implements
Examples
string password = GetPasswordFromSecureSource();
string fileName = Path.Combine(Path.GetTempPath(), Path.ChangeExtension(Path.GetRandomFileName(), ".xlsx"));
XlsxSaveOptions options = new XlsxSaveOptions();
options.Password = password;
workbook.Save(fileName, options);
bool isEncrypted = workbook.IsEncryptedFile(fileName);
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.