How to detect the Line Ending Type of a Text File in C# .NET Framework

Line endings are a lurking stumbling block in so many projects. I can’t tell you the number of times I have tried bringing in an outside asset, only to run into problems with stupid line endings, oftentimes hours later. It is a much better approach to simply detect the line endings of a file in advance, so they can be handled before any problems can arise. This can be done very easily with the line ending detection method that I am about to demonstrate.

We begin with our package installation:

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

Now we call our function:

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 EditTextDetectLineEndingsExample{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 EditTextApi();var inputFile = new System.IO.Stream(); // System.IO.Stream | Input file to perform the operation on.try{// Detect line endings of a text fileDetectLineEndingsResponse result = apiInstance.EditTextDetectLineEndings(inputFile);Debug.WriteLine(result);}catch (Exception e){Debug.Print("Exception when calling EditTextApi.EditTextDetectLineEndings: " + e.Message );}}}}

And you’re done! There is another function in this library that can also change the line endings to fit your needs.

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