Как определить формат файла и проверить, зашифрован ли файл
Contents
[
Hide
]
Иногда вам может потребоваться определить формат файла перед его открытием, потому что расширение файла не гарантирует соответствие содержимого файла. Файл может быть зашифрован (защищен паролем), поэтому его нельзя прочитать непосредственно, или его не следует читать. Aspose.Cells предоставляет статический метод FileFormatUtil.DetectFileFormat() и некоторые связанные API, которые вы можете использовать для обработки документов.
В следующем образце кода показано, как определить формат файла (с использованием пути к файлу) и проверить его расширение. Вы также можете определить, зашифрован ли файл.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
//Detect file format | |
FileFormatInfo info = FileFormatUtil.DetectFileFormat(dataDir + "Book1.xlsx"); | |
//Gets the detected load format | |
Console.WriteLine("The spreadsheet format is: " + FileFormatUtil.LoadFormatToExtension(info.LoadFormat)); | |
//Check if the file is encrypted. | |
Console.WriteLine("The file is encrypted: " + info.IsEncrypted); |