Class FileHandler
- Namespace
- NominalSystems.Core
- Assembly
- NominalSystems.Core.dll
This class is a helper static class for managing files that may exist or creating new files in directories. It adds additional functionality to files and is able to determine what files exist within a directory.
public static class FileHandler
- Inheritance
-
FileHandler
Methods
CreateDirectory(string)
Constructs a directory at the current path. This will force a directory to be constructed and works with subdirectories too.
public static bool CreateDirectory(string path)
Parameters
path
stringThe full path to create the directories of
Returns
- bool
A flag that indicates a new directory was created
CreateFile(string)
Attempts to create a file with a specified path. This will create all directories that are needed if required. If the file is unable to be created, this will return a fail flag.
public static bool CreateFile(string path)
Parameters
path
stringThe full file path to the new file
Returns
- bool
A successful creation flag
CreateFile(string, string)
Attempts to create a file with a specified directory and a file name.
public static bool CreateFile(string directory, string file)
Parameters
directory
stringThe full path to the directory to add the file to
file
stringThe name of the file to add
Returns
- bool
A successful file creation flag
GetFileDirectory(string)
Returns the full path to the directory specified by the file path that exists. This will remove the file extension at the end of the path and just return the full path to the directory.
public static string GetFileDirectory(string path)
Parameters
path
stringThe full or relative path to some file
Returns
- string
The full path to the directory the file exists in
GetFullPath(string)
Returns the full file path to a particular file that is not relative to a particular asset or the current working directory.
public static string GetFullPath(string path)
Parameters
path
stringThe path to fetch the full path for
Returns
- string
The full file path from the device base directory
GetSize(string)
Returns the size of the file or directory that exists as the number of bytes. For files, this is the total size of the file. For directories, this is the total size of the directory with all subdirectories included.
public static long GetSize(string path)
Parameters
path
stringThe full or relative path to a file
Returns
- long
The size of the path in bytes
IsValidPath(string)
Returns whether a particular path is valid, either to a directory or a file path. This will also check the Asset Paths to see if there is a matching relative asset path.
public static bool IsValidPath(string path)
Parameters
path
stringThe full or relative path to an asset registry
Returns
- bool
A valid path flag
ListDirectories(string, string, bool, bool)
Lists all directories that exist within a directory and are able to find the directories recursively and with a filter if required. The relative path will return the directory with their path relative to the input path, otherwise will return the full directory path. The filter is a regex filter that will be able to filter based on the search condition. If the path is invalid to a directory, then there will be no directories listed.
public static string[] ListDirectories(string path, string filter = "*", bool recursive = false, bool relative = false)
Parameters
path
stringThe full or relative directory path
filter
stringA Regex filter to sort the directories that will be returned
recursive
boolA flag whether to recursively fetch all directories down the tree
relative
boolA flag whether to return just the relative path to the directory of each file
Returns
- string[]
A list of all directories that meet the conditions above
ListFiles(string, string, bool, bool)
Lists all files that exist within a directory and is able to find the files recursively and with a filter if required. The relative path will return the files with their path relative to the input path, otherwise will return the full file path. The filter is a regex filter that will be able to filter based on the search condition. If the path is invalid to a directory, then there will be no files listed.
public static string[] ListFiles(string path, string filter = "*", bool recursive = false, bool relative = false)
Parameters
path
stringThe full or relative directory path
filter
stringA Regex filter to sort the files that will be returned
recursive
boolA flag whether to recursively fetch all files down the tree
relative
boolA flag whether to return just the relative path to the directory of each file
Returns
- string[]
A list of all file paths that meet the conditions above
ReadLines(string)
Reads all lines from a particular file and splits the lines by the new line character. If the path is invalid, then no lines will be returned. This will be an array of strings.
public static string[] ReadLines(string path)
Parameters
path
stringThe full or relative path to the file
Returns
- string[]
The array of lines found inside the file
ReadText(string)
Reads all text from a particular file and returns the text as a string back to the caller. If the path is invalid, then an empty string will be returned.
public static string ReadText(string path)
Parameters
path
stringThe full or relative path to the file
Returns
- string
The full text found within the file path
WriteText(string, string, bool)
Attempts to write some new text to a particular file. If the file does not exist, it will create a new file. This text can also be appended to the end of an existing file or clear the file and write the text otherwise.
public static bool WriteText(string path, string text, bool append = false)
Parameters
path
stringThe full or relative path to the file
text
stringThe text to add to the file
append
boolA flag whether to add it to the end of the file
Returns
- bool
A successful write flag