Determine the file type

View as MarkdownMarkdown

Set up your AI agent

Give your coding agent live access to the GroupDocs documentation and API reference through the GroupDocs Docs MCP server — no install, no API key. Learn more

claude mcp add --transport http groupdocs-docs https://docs.groupdocs.com/mcp

Also process documents locally

The GroupDocs.Viewer MCP server lets your agent work with files on your machine; they are never uploaded. It needs the .NET 10 SDK or Docker.

claude mcp add groupdocs-viewer -- dnx GroupDocs.Viewer.Mcp --yes
Folders, licensing and more →

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:

Determine the file type by the file extension

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:

using System;
using GroupDocs.Viewer;
// ...

// Specify a file extension.
string extension = ".docx";
// Set a file type using the extension.
FileType fileType = FileType.FromExtension(extension);
// Display the extension and the file type.
Console.WriteLine($"Extension {extension}; File type: {fileType}.");
Imports System
Imports GroupDocs.Viewer
' ...

Module Program
    Sub Main(args As String())
        ' Specify a file extension.
        Dim extension As String = ".docx"
        ' Set a file type using the extension.
        Dim fileType As FileType = FileType.FromExtension(extension)
        ' Display the extension and the file type.
        Console.WriteLine($"Extension {extension}; File type: {fileType}.")
    End Sub
End Module
Extension .docx; File type: Microsoft Word Open XML Document (.docx).

Download full 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:

using System;
using GroupDocs.Viewer;
// ...

string mediaType = "application/pdf";
FileType fileType = FileType.FromMediaType(mediaType);
Console.WriteLine($"Media-type {mediaType}; File type: {fileType}.");
Imports System
Imports GroupDocs.Viewer
' ...

Module Program
    Sub Main(args As String())
        Dim mediaType As String = "application/pdf"
        Dim fileType As FileType = FileType.FromMediaType(mediaType)
        Console.WriteLine($"Media-type {mediaType}; File type: {fileType}.")
    End Sub
End Module
Media-type application/pdf; File type: Portable Document Format File (.pdf).

Download full 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:

using System;
using System.IO;
using GroupDocs.Viewer;
// ...

using (Stream stream = File.OpenRead("sample.docx"))
{
    FileType fileType = FileType.FromStream(stream);
    Console.WriteLine($"File type: {fileType}.");
}
Imports System
Imports System.IO
Imports GroupDocs.Viewer
' ...

Module Program
    Sub Main(args As String())
        Using stream As Stream = File.OpenRead("sample.docx")
            Dim fileType As FileType = FileType.FromStream(stream)
            Console.WriteLine($"File type: {fileType}.")
        End Using
    End Sub
End Module

sample.docx is the sample file used in this example. Click here to download it.

File type: Microsoft Word Open XML Document (.docx).

Download full output