Class TextFileHelper
- Namespace
- Dynamicweb.Core.Helpers
- Assembly
- Dynamicweb.Core.dll
Provides utility methods for reading and writing text files with built-in error handling, retry mechanisms, and support for file encoding. This class is designed to simplify common file operations while ensuring robust handling of file system limitations, such as access permissions or transient I/O errors.
public class TextFileHelper
- Inheritance
-
TextFileHelper
- Inherited Members
Remarks
The TextFileHelper class includes methods for reading and writing text files, both in standard mode and with retry logic to handle common issues like file locks or temporary unavailability. It also ensures that directories are created as needed, and files are made writable before write operations.
Features of this class include:
- Reading and writing text files with optional encoding.
- Retry mechanisms for transient errors during file access.
- Automatic creation of missing directories when writing files.
- Support for appending or overwriting files.
- Detailed logging for failures, including permissions, I/O errors, and invalid file attributes.
Methods
ReadTextFile(string)
Returns the contents of a text file as a string.
public static string ReadTextFile(string path)
Parameters
pathstringPath and name of text file to read. E.g. c:\path\file.txt
Returns
Remarks
Path must contain the physical path to the file e.g. "C:\path\file.txt"
- See Also
ReadTextFile(string, Encoding)
Read the current text file in a particular encoding.
public static string ReadTextFile(string path, Encoding encoding)
Parameters
pathstringPath and name of text file to read. E.g. c:\path\file.txt
encodingEncodingAs System.Text.Encoding, to determine a particular encoding.
Returns
- string
Returns the contents of a text file as a string.
Remarks
Path must contain the physical path to the file e.g. "C:\path\file.txt"
- See Also
TryMakeWritable(FileInfo)
Tries to make the given file writable.
public static bool TryMakeWritable(FileInfo info)
Parameters
infoFileInfoThe file information (FileInfo instance).
Returns
- bool
True if the file is successfully made writable, or if it was already writable; otherwise, false.
WriteTextFile(string, string, bool)
Writes a string to a text file.
public static void WriteTextFile(string value, string path, bool appendToFile = false)
Parameters
valuestringString to write to the file.
pathstringPath and name of text file to write. E.g. c:\path\file.txt
appendToFileboolBoolean indicating whether to append to the file. True to append, False to overwrite file.
Remarks
Path must contain the physical file path to the file e.g. SystemInformation.MapPath("/files/filer/file.txt")
- See Also
WriteTextFile(string, string, bool, Encoding)
Writes a string to a text file in a particular encoding.
public static void WriteTextFile(string value, string path, bool appendToFile, Encoding encoding)
Parameters
valuestringString to write to the file.
pathstringPath and name of text file to write.
appendToFileboolBoolean indicating whether to append to the file. True to append, False to overwrite file.
encodingEncodingAs System.Text.Encoding, to determine a particular encoding.
Remarks
Path must contain the physical file path to the file e.g. SystemInformation.MapPath("/files/filer/file.txt")
- See Also