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.
- Inheritance
-
Text
File Helper
- Inherited Members
Remarks
The Text
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.
Parameters
path
stringPath 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.
Parameters
path
stringPath and name of text file to read. E.g. c:\path\file.txt
encoding
EncodingAs 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.
Parameters
info
FileInfo The 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.
Parameters
value
stringString to write to the file.
path
stringPath and name of text file to write. E.g. c:\path\file.txt
appendToFile
boolBoolean 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.
Parameters
value
stringString to write to the file.
path
stringPath and name of text file to write.
appendToFile
boolBoolean indicating whether to append to the file. True to append, False to overwrite file.
encoding
EncodingAs 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