A file type is a standard way that information is encoded for storage in a computer file. For example, Microsoft Word (.docx) and Adobe PDF (.pdf) are two different file types.
Using GroupDocs.Viewer, you can determine the file type in one of the following ways:
To determine the file type from the file extension, use the FromExtension() method of FileType.
The following code snippet shows how to determine a file type using the file extension:
usingSystem;usingGroupDocs.Viewer;// ...// Specify a file extension.stringextension=".docx";// Set a file type using the extension.FileTypefileType=FileType.FromExtension(extension);// Display the extension and the file type.Console.WriteLine($"Extension {extension}; File type: {fileType}.");
ImportsSystemImportsGroupDocs.Viewer' ...
ModuleProgramSubMain(argsAsString())' Specify a file extension.
DimextensionAsString=".docx"' Set a file type using the extension.
DimfileTypeAsFileType=FileType.FromExtension(extension)' Display the extension and the file type.
Console.WriteLine($"Extension {extension}; File type: {fileType}.")EndSubEndModule
The following image shows a sample console output:
Determine file type by media type
If you receive a file via the Internet, you can check its media type header using the FromMediaType() method of the FileType class.
The following code snippet shows how to determine a file type using the media header:
The following image shows a sample console output:
Determine file type by the stream
When you do not know a file extension or media type header, you can try determining file type by using the FromStream() method of the FileType class. GroupDocs.Viewer will try reading the signature and mapping it to the file type.
The following code snippet shows how to determine a file type using the file signature: