File Controller

Create Class

Create a static class called FileController in the Controller namespace

See: Classes

namespace Library.Controller;
public static class FileController
{}

Methods

Get Encoding

create a public static method named GetEncoding that takes a System.IO.FileStream as a parameter and returns System.Text.Encoding

public static Encoding GetEncoding(FileStream fs){}

create a byte buffer

byte[] buffer = new byte[4];

read the first 4 bytes from the file and populate the buffer with them

Check if the first 4 bytes contain Unicode characters

Check if it contains UTF-16

Check if it contains UTF-8

Otherwise return ASCII

Overview

Is Text

This checks if the encoding type is text-based.

Get File

This method takes in a string for a path and returns File Model

First create a FileInfo variable named info

Next initialize the default values

Next create a file stream opening the file and reading it

Now get the files encoding type

See: Get Encoding

Check if the encoding type is text

Create a stream reader and read the contents of the file

Now get the number of lines and number of characters

Now close the File Stream

Finally return the resulting File Model

Overview

Overview

Last updated

Was this helpful?