How to validate any File Type using Autodetect in C# .NET Framework

File validation is incredibly useful and will save you a ton of heartache down the line. But what do you do when you have a whole mess of different file types jumbled together? It’s difficult enough setting up a means of validating one format, much less identifying (often mislabeled) different formats and having separate validation logic for each. That’s a true challenge and I’m going to go ahead and assume that you don’t have time to deal with it. Today I will be showing you the easy way to get this done.

First we will need our package:

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

Now we can go ahead and drop our function call in like this:

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 ValidateDocumentAutodetectValidationExample{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 ValidateDocumentApi();var inputFile = new System.IO.Stream(); // System.IO.Stream | Input file to perform the operation on.try{// Autodetect content type and validateAutodetectDocumentValidationResult result = apiInstance.ValidateDocumentAutodetectValidation(inputFile);Debug.WriteLine(result);}catch (Exception e){Debug.Print("Exception when calling ValidateDocumentApi.ValidateDocumentAutodetectValidation: " + e.Message );}}}}

And the API will automatically determine the file format and send back the validation results. Simple.

Image for post
Image for post

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