How to get document type information in C# .NET Framework

It is all to common to run across files with incorrect file extensions, or even no extensions at all. Identifying these problem files is tedious enough to do manually, so don’t even get me started on setting up a function for this in .NET Framework. It’s your lucky day, though. We have a much easier solution to try out.

Ok, first thing’s first. We need to install our API client. Simply go to your Package Manager console and activate this command:

Install-Package Cloudmersive.APIClient.NET.DocumentAndDataConvert -Version 2.2.0

Next we can call ConvertDocumentAutodetectGetInfo, specify a file to identify, and let the API go to work.

using System;using System.Diagnostics;using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Api;using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Client;using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Model;namespace Example{public class ConvertDocumentAutodetectGetInfoExample{public void main(){// Configure API key authorization: ApikeyConfiguration.Default.AddApiKey("Apikey", "YOUR_API_KEY");// Uncomment below to setup prefix (e.g. Bearer) for API key, if needed// Configuration.Default.AddApiKeyPrefix("Apikey", "Bearer");var apiInstance = new ConvertDocumentApi();var inputFile = new System.IO.Stream(); // System.IO.Stream | Input file to perform the operation on.try{// Get document type informationAutodetectGetInfoResult result = apiInstance.ConvertDocumentAutodetectGetInfo(inputFile);Debug.WriteLine(result);}catch (Exception e){Debug.Print("Exception when calling ConvertDocumentApi.ConvertDocumentAutodetectGetInfo: " + e.Message );}}}}

That’s all there is to it! Our data will be presented to us in this format:

"Successful": true,
"DetectedFileExtension": "string",
"DetectedMimeType": "string",
"PageCount": 0,
"Author": "string",
"DateModified": "2020-02-03T04:52:11.076Z",
"AlternateFileTypeCandidates": [
"Probability": 0,
"DetectedFileExtension": "string",
"DetectedMimeType": "string"
Image for post

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store