Table of Contents

Class TF

Namespace
SunamoFileIO
Assembly
SunamoFileIO.dll

Text File helper class providing file I/O operations.

public class TF
Inheritance
TF
Inherited Members
Extension Methods

Fields

BomUtf8

UTF-8 BOM (Byte Order Mark) bytes: 239, 187, 191.

public static readonly List<byte> BomUtf8

Field Value

List<byte>

Properties

IsUsed

Function to check if a file is currently in use.

public static Func<string, bool>? IsUsed { get; set; }

Property Value

Func<string, bool>

ReadFile

Enable file reading operations.

public static bool ReadFile { get; set; }

Property Value

bool

ThrowExcIfCannotBeWritten

Throw exception if file cannot be written.

public static bool ThrowExcIfCannotBeWritten { get; set; }

Property Value

bool

Methods

AppendAllLines(string, IEnumerable<string>, bool)

Appends lines to a file, optionally removing duplicates.

public static Task AppendAllLines(string filePath, IEnumerable<string> linesToAppend, bool isDuplicatingRemoving = false)

Parameters

filePath string

Path to the file.

linesToAppend IEnumerable<string>

Lines to append to file.

isDuplicatingRemoving bool

Whether to remove duplicate lines after appending.

Returns

Task

AppendAllLinesSync(string, List<string>)

Appends lines to a file synchronously.

public static void AppendAllLinesSync(string path, List<string> lines)

Parameters

path string

Path to the file.

lines List<string>

Lines to append to file.

AppendAllText(string, string)

Appends text to a file, creating it if it doesn't exist.

public static Task AppendAllText(string path, string content)

Parameters

path string

Path to the file.

content string

Content to append to file.

Returns

Task

AppendAllTextSync(string, string)

Appends text to a file synchronously.

public static void AppendAllTextSync(string path, string content)

Parameters

path string

Path to the file.

content string

Content to append to file.

Copy(string, string, bool)

Copies a file from source to destination.

public static void Copy(string source, string destination, bool isOverwriting = false)

Parameters

source string

Source file path.

destination string

Destination file path.

isOverwriting bool

Whether to overwrite if destination exists.

CreateEmptyFileWhenDoesntExists(string)

Creates an empty file if it doesn't exist.

public static Task CreateEmptyFileWhenDoesntExists(string path)

Parameters

path string

Path to the file to create.

Returns

Task

Delete(string)

Deletes a file.

public static void Delete(string path)

Parameters

path string

Path to file to delete.

Exists(string)

Checks if a file exists.

public static bool Exists(string path)

Parameters

path string

Path to check.

Returns

bool

True if file exists, false otherwise.

GetEncoding(FileStream)

Gets encoding from file stream by reading its BOM (Byte Order Mark). Note: May not work correctly with all files - Air bank export returns US-ascii/1252 but file has diacritics. Atom with auto-encoding returns ISO-8859-2 which is correct.

public static Encoding GetEncoding(FileStream file)

Parameters

file FileStream

File stream to read from.

Returns

Encoding

Detected encoding.

GetEncoding(string)

Gets encoding from file by reading its BOM (Byte Order Mark).

public static Encoding GetEncoding(string filename)

Parameters

filename string

Path to the file.

Returns

Encoding

Detected encoding.

GetNumberOfLinesTrimEnd(string)

Gets the line number of the last non-empty line (after trimming).

public static Task<int> GetNumberOfLinesTrimEnd(string filePath)

Parameters

filePath string

Path to the file.

Returns

Task<int>

Line number of last non-empty line, or 0 if all lines are empty.

LockedByBitLocker(string)

Checks if a path is locked by BitLocker.

protected static bool LockedByBitLocker(string path)

Parameters

path string

Path to check.

Returns

bool

Always returns false (BitLocker check not implemented).

Move(string, string, bool)

Moves a file from source to destination.

public static void Move(string source, string destination, bool isOverwriting = false)

Parameters

source string

Source file path.

destination string

Destination file path.

isOverwriting bool

Whether to overwrite if destination exists.

PureFileOperation(string, Func<string, string>)

Applies a transformation function to file content and writes back if content changed.

public static Task<bool> PureFileOperation(string filePath, Func<string, string> transformFunction)

Parameters

filePath string

Path to the file.

transformFunction Func<string, string>

Function that transforms file content.

Returns

Task<bool>

True if file was modified, false otherwise.

PureFileOperation(string, Func<string, string>, string)

Applies a transformation function to file content and writes to a new file with inserted text in filename if content changed.

public static Task<bool> PureFileOperation(string filePath, Func<string, string> transformFunction, string insertBetweenFilenameAndExtension)

Parameters

filePath string

Path to the file.

transformFunction Func<string, string>

Function that transforms file content.

insertBetweenFilenameAndExtension string

Text to insert between filename and extension for output file.

Returns

Task<bool>

True if file was modified, false otherwise.

PureFileOperationWithArg(string, Func<string, string, string>, string)

Applies a transformation function to file content and writes back if content changed.

public static Task<bool> PureFileOperationWithArg(string filePath, Func<string, string, string> transformFunction, string argument)

Parameters

filePath string

Path to the file.

transformFunction Func<string, string, string>

Function that transforms file content with an argument.

argument string

Argument to pass to transformation function.

Returns

Task<bool>

True if file was modified, false otherwise.

ReadAllBytes(string)

Reads all bytes from a file.

public static Task<List<byte>> ReadAllBytes(string filePath)

Parameters

filePath string

Path to the file.

Returns

Task<List<byte>>

List of bytes from file, or empty list if locked by BitLocker.

ReadAllBytesArray(string)

Reads all bytes from file and returns as byte array.

public static Task<byte[]> ReadAllBytesArray(string path)

Parameters

path string

Path to the file.

Returns

Task<byte[]>

Array of bytes from file.

ReadAllBytesSync(string)

Reads all bytes from a file synchronously.

public static List<byte> ReadAllBytesSync(string path)

Parameters

path string

Path to the file.

Returns

List<byte>

List of bytes from file.

ReadAllLines(string, bool)

Reads all lines from a file, optionally trimming empty lines.

public static Task<List<string>> ReadAllLines(string filePath, bool isTrimmingEmptyLines = true)

Parameters

filePath string

Path to the file.

isTrimmingEmptyLines bool

Whether to remove empty or whitespace-only lines.

Returns

Task<List<string>>

List of lines from file.

ReadAllLinesArray(string)

Reads all lines from file and returns as string array.

public static Task<string[]> ReadAllLinesArray(string path)

Parameters

path string

Path to the file.

Returns

Task<string[]>

Array of lines from file.

ReadAllLinesSync(string)

Reads all lines from a file synchronously.

public static List<string> ReadAllLinesSync(string path)

Parameters

path string

Path to the file.

Returns

List<string>

List of lines from file.

ReadAllText(string, Encoding?)

Reads all text from a file, creating it if it doesn't exist.

public static Task<string> ReadAllText(string path, Encoding? encoding = null)

Parameters

path string

Path to the file.

encoding Encoding

Encoding to use (defaults to UTF-8).

Returns

Task<string>

Content of the file, or empty string if file doesn't exist or is locked.

ReadAllTextSync(string)

Reads all text from a file synchronously.

public static string ReadAllTextSync(string path)

Parameters

path string

Path to the file.

Returns

string

Content of the file, or empty string if file doesn't exist.

ReadConfigLines(string)

Reads configuration file lines, excluding comment lines starting with #.

public static Task<List<string>> ReadConfigLines(string configFilePath)

Parameters

configFilePath string

Path to configuration file.

Returns

Task<List<string>>

List of non-comment lines.

ReadFileOrReturn(string)

Reads file content if path is valid and shorter than 250 characters, otherwise returns the input string.

public static object ReadFileOrReturn(string pathOrText)

Parameters

pathOrText string

File path or text content.

Returns

object

File content if path exists and is valid, otherwise returns input string.

ReadFileParallel(string, IList<string>, IList<string>)

Reads file and performs parallel string replacements with default line count of 1470.

public static string ReadFileParallel(string fileName, IList<string> searchStrings, IList<string> replacementStrings)

Parameters

fileName string

Path to file to read.

searchStrings IList<string>

List of strings to search for.

replacementStrings IList<string>

List of replacement strings.

Returns

string

Empty string.

ReadFileParallel(string, int, IList<string>, IList<string>)

Reads file and performs parallel string replacements.

public static string ReadFileParallel(string fileName, int linesCount, IList<string> searchStrings, IList<string> replacementStrings)

Parameters

fileName string

Path to file to read.

linesCount int

Expected number of lines in file.

searchStrings IList<string>

List of strings to search for.

replacementStrings IList<string>

List of replacement strings.

Returns

string

Empty string.

RemoveDoubleBomUtf8(string)

Removes double UTF-8 BOM from file if present.

public static Task RemoveDoubleBomUtf8(string path)

Parameters

path string

Path to the file.

Returns

Task

Replace(string, string, string)

Replaces text in a file.

public static Task Replace(string filePath, string searchText, string replacementText)

Parameters

filePath string

Path to the file.

searchText string

Text to search for.

replacementText string

Text to replace with.

Returns

Task

TextReader(string)

Opens a text file and returns a StreamReader. StreamReader is derived from TextReader.

public static StreamReader TextReader(string filePath)

Parameters

filePath string

Path to the file to open.

Returns

StreamReader

StreamReader for reading the file.

WaitD()

Waits and returns empty string (async helper).

public static Task<string> WaitD()

Returns

Task<string>

Empty string.

WriteAllBytes(string, IEnumerable<byte>)

Writes all bytes to a file.

public static Task WriteAllBytes(string filePath, IEnumerable<byte> bytes)

Parameters

filePath string

Path to the file.

bytes IEnumerable<byte>

Bytes to write to file.

Returns

Task

WriteAllBytesArray(string, byte[])

Writes all bytes from byte array to file.

public static Task WriteAllBytesArray(string path, byte[] bytes)

Parameters

path string

Path to the file.

bytes byte[]

Array of bytes to write.

Returns

Task

WriteAllBytesSync(string, List<byte>)

Writes all bytes to a file synchronously.

public static void WriteAllBytesSync(string path, List<byte> bytes)

Parameters

path string

Path to the file.

bytes List<byte>

Bytes to write to file.

WriteAllLines(string, IList<string>)

Writes all lines to a file.

public static Task WriteAllLines(string filePath, IList<string> lines)

Parameters

filePath string

Path to the file.

lines IList<string>

Lines to write to file.

Returns

Task

WriteAllLinesArray(string, string[])

Writes all lines from string array to file.

public static Task WriteAllLinesArray(string path, string[] lines)

Parameters

path string

Path to the file.

lines string[]

Array of lines to write.

Returns

Task

WriteAllLinesSync(string, List<string>)

Writes all lines to a file synchronously.

public static void WriteAllLinesSync(string path, List<string> lines)

Parameters

path string

Path to the file.

lines List<string>

Lines to write to file.

WriteAllText(string, string, bool)

Writes or appends text to a file with Unix line endings.

public static Task WriteAllText(string path, string content, bool isAppending)

Parameters

path string

Path to the file.

content string

Content to write or append.

isAppending bool

Whether to append to file instead of overwriting.

Returns

Task

WriteAllText(string, string, Encoding?)

Writes all text to a file.

public static Task WriteAllText(string path, string content, Encoding? encoding = null)

Parameters

path string

Path to the file.

content string

Content to write to file.

encoding Encoding

Encoding to use (defaults to UTF-8).

Returns

Task

WriteAllText(string, StringBuilder)

Writes all text from StringBuilder to a file with Unix line endings.

public static Task WriteAllText(string path, StringBuilder stringBuilder)

Parameters

path string

Path to the file.

stringBuilder StringBuilder

StringBuilder containing content to write.

Returns

Task

WriteAllTextSync(string, string)

Writes text to a file synchronously.

public static void WriteAllTextSync(string path, string content)

Parameters

path string

Path to the file.

content string

Content to write to file.