How to set or change line endings of a text file in C# .NET Framework

With today’s quick and easy API lesson, you will be able to set a text file’s line endings to suit Windows, Mac, or Linux. Let’s dive straight in.

First up to the plate, client installation. This can be accomplished via the Package console and this command.

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

Moving right along, let’s feed our input file into EditTextChangeLineEndings and choose which format we want.

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 EditTextChangeLineEndingsExample{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 lineEndingType = lineEndingType_example;  // string | Required; 'Windows' will use carriage return and line feed, 'Unix' will use newline, and 'Mac' will use carriage returnvar inputFile = new System.IO.Stream(); // System.IO.Stream | Input file to perform the operation on.try{// Set, change line endings of a text fileChangeLineEndingResponse result = apiInstance.EditTextChangeLineEndings(lineEndingType, inputFile);Debug.WriteLine(result);}catch (Exception e){Debug.Print("Exception when calling EditTextApi.EditTextChangeLineEndings: " + e.Message );}}}}

And that’s a wrap. Our response will be, simply, our text file with the new line endings. If this solution worked for you, take a look at the rest of this tutorial series for many more useful functions like this.

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