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
Properties
IsUsed
Function to check if a file is currently in use.
public static Func<string, bool>? IsUsed { get; set; }
Property Value
ReadFile
Enable file reading operations.
public static bool ReadFile { get; set; }
Property Value
ThrowExcIfCannotBeWritten
Throw exception if file cannot be written.
public static bool ThrowExcIfCannotBeWritten { get; set; }
Property Value
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
filePathstringPath to the file.
linesToAppendIEnumerable<string>Lines to append to file.
isDuplicatingRemovingboolWhether to remove duplicate lines after appending.
Returns
AppendAllLinesSync(string, List<string>)
Appends lines to a file synchronously.
public static void AppendAllLinesSync(string path, List<string> lines)
Parameters
AppendAllText(string, string)
Appends text to a file, creating it if it doesn't exist.
public static Task AppendAllText(string path, string content)
Parameters
Returns
AppendAllTextSync(string, string)
Appends text to a file synchronously.
public static void AppendAllTextSync(string path, string content)
Parameters
Copy(string, string, bool)
Copies a file from source to destination.
public static void Copy(string source, string destination, bool isOverwriting = false)
Parameters
sourcestringSource file path.
destinationstringDestination file path.
isOverwritingboolWhether to overwrite if destination exists.
CreateEmptyFileWhenDoesntExists(string)
Creates an empty file if it doesn't exist.
public static Task CreateEmptyFileWhenDoesntExists(string path)
Parameters
pathstringPath to the file to create.
Returns
Delete(string)
Deletes a file.
public static void Delete(string path)
Parameters
pathstringPath to file to delete.
Exists(string)
Checks if a file exists.
public static bool Exists(string path)
Parameters
pathstringPath 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
fileFileStreamFile 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
filenamestringPath 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
filePathstringPath to the file.
Returns
LockedByBitLocker(string)
Checks if a path is locked by BitLocker.
protected static bool LockedByBitLocker(string path)
Parameters
pathstringPath 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
sourcestringSource file path.
destinationstringDestination file path.
isOverwritingboolWhether 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
filePathstringPath to the file.
transformFunctionFunc<string, string>Function that transforms file content.
Returns
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
filePathstringPath to the file.
transformFunctionFunc<string, string>Function that transforms file content.
insertBetweenFilenameAndExtensionstringText to insert between filename and extension for output file.
Returns
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
filePathstringPath to the file.
transformFunctionFunc<string, string, string>Function that transforms file content with an argument.
argumentstringArgument to pass to transformation function.
Returns
ReadAllBytes(string)
Reads all bytes from a file.
public static Task<List<byte>> ReadAllBytes(string filePath)
Parameters
filePathstringPath to the file.
Returns
ReadAllBytesArray(string)
Reads all bytes from file and returns as byte array.
public static Task<byte[]> ReadAllBytesArray(string path)
Parameters
pathstringPath to the file.
Returns
ReadAllBytesSync(string)
Reads all bytes from a file synchronously.
public static List<byte> ReadAllBytesSync(string path)
Parameters
pathstringPath to the file.
Returns
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
filePathstringPath to the file.
isTrimmingEmptyLinesboolWhether to remove empty or whitespace-only lines.
Returns
ReadAllLinesArray(string)
Reads all lines from file and returns as string array.
public static Task<string[]> ReadAllLinesArray(string path)
Parameters
pathstringPath to the file.
Returns
ReadAllLinesSync(string)
Reads all lines from a file synchronously.
public static List<string> ReadAllLinesSync(string path)
Parameters
pathstringPath to the file.
Returns
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
Returns
ReadAllTextSync(string)
Reads all text from a file synchronously.
public static string ReadAllTextSync(string path)
Parameters
pathstringPath 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
configFilePathstringPath to configuration file.
Returns
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
pathOrTextstringFile 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
fileNamestringPath to file to read.
searchStringsIList<string>List of strings to search for.
replacementStringsIList<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
fileNamestringPath to file to read.
linesCountintExpected number of lines in file.
searchStringsIList<string>List of strings to search for.
replacementStringsIList<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
pathstringPath to the file.
Returns
Replace(string, string, string)
Replaces text in a file.
public static Task Replace(string filePath, string searchText, string replacementText)
Parameters
filePathstringPath to the file.
searchTextstringText to search for.
replacementTextstringText to replace with.
Returns
TextReader(string)
Opens a text file and returns a StreamReader. StreamReader is derived from TextReader.
public static StreamReader TextReader(string filePath)
Parameters
filePathstringPath 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
WriteAllBytes(string, IEnumerable<byte>)
Writes all bytes to a file.
public static Task WriteAllBytes(string filePath, IEnumerable<byte> bytes)
Parameters
filePathstringPath to the file.
bytesIEnumerable<byte>Bytes to write to file.
Returns
WriteAllBytesArray(string, byte[])
Writes all bytes from byte array to file.
public static Task WriteAllBytesArray(string path, byte[] bytes)
Parameters
Returns
WriteAllBytesSync(string, List<byte>)
Writes all bytes to a file synchronously.
public static void WriteAllBytesSync(string path, List<byte> bytes)
Parameters
WriteAllLines(string, IList<string>)
Writes all lines to a file.
public static Task WriteAllLines(string filePath, IList<string> lines)
Parameters
Returns
WriteAllLinesArray(string, string[])
Writes all lines from string array to file.
public static Task WriteAllLinesArray(string path, string[] lines)
Parameters
Returns
WriteAllLinesSync(string, List<string>)
Writes all lines to a file synchronously.
public static void WriteAllLinesSync(string path, List<string> lines)
Parameters
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
pathstringPath to the file.
contentstringContent to write or append.
isAppendingboolWhether to append to file instead of overwriting.
Returns
WriteAllText(string, string, Encoding?)
Writes all text to a file.
public static Task WriteAllText(string path, string content, Encoding? encoding = null)
Parameters
pathstringPath to the file.
contentstringContent to write to file.
encodingEncodingEncoding to use (defaults to UTF-8).
Returns
WriteAllText(string, StringBuilder)
Writes all text from StringBuilder to a file with Unix line endings.
public static Task WriteAllText(string path, StringBuilder stringBuilder)
Parameters
pathstringPath to the file.
stringBuilderStringBuilderStringBuilder containing content to write.
Returns
WriteAllTextSync(string, string)
Writes text to a file synchronously.
public static void WriteAllTextSync(string path, string content)