Class FileHandler
- Namespace
- NominalSystems.Files
- Assembly
- NominalSystems.Files.dll
[Nominal] 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
Properties
ComputerDirectory
Returns the full path to the 'My Computer' directory on the device.
public static string ComputerDirectory { get; }
Property Value
- string
The full path to the directory
DesktopDirectory
Returns the full path to the 'Desktop' directory on the device.
public static string DesktopDirectory { get; }
Property Value
- string
The full path to the directory
DocumentsDirectory
Returns the full path to the 'My Documents' directory on the device.
public static string DocumentsDirectory { get; }
Property Value
- string
The full path to the directory
DownloadsDirectory
Returns the full path to the 'Downloads' directory on the device.
public static string DownloadsDirectory { get; }
Property Value
- string
The full path to the directory
Methods
CopyDirectory(string, string, bool)
Duplicates a directory from one location to another. This will overwrite any existing directory at the destination path.
public static bool CopyDirectory(string from, string to, bool overwrite = true)
Parameters
from
stringThe full directory path of the directory to duplicate
to
stringThe full directory path of the directory to duplicate to
overwrite
boolA flag whether to overwrite the data if it already exists
Returns
- bool
A successful duplication flag
CopyFile(string, string, bool)
Duplicates a file from one location to another. This will overwrite any existing file at the destination path.
public static bool CopyFile(string from, string to, bool overwrite = true)
Parameters
from
stringThe full file path of the file to duplicate
to
stringThe full file path of the file to duplicate to
overwrite
boolA flag whether to overwrite the data if it already exists
Returns
- bool
A successful duplication flag
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
DeleteDirectory(string)
Attempts to delete a directory that exists at the specified path. This will delete all files within the directory and the directory itself if it has the correct permissions and exists.
public static bool DeleteDirectory(string path)
Parameters
path
stringThe full path to the directory
Returns
- bool
A successful deletion
DeleteFile(string)
Attempts to delete a file that exists at the specified path. This will remove it if it has the correct permissions and exists.
public static bool DeleteFile(string path)
Parameters
path
stringThe full path to the file
Returns
- bool
A successful deletion
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
GetFileName(string)
Returns the file name from a particular path. This is the last part of the path that is the file name.
public static string GetFileName(string path)
Parameters
path
stringThe full path to the file, regardless if it exists
Returns
- string
The name of the file
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
Rename(string, string)
Renames a file or directory at the specified path. If the new name is just the end of the path, then the rest of the path will be added.
public static bool Rename(string path, string newName)
Parameters
path
stringThe full or relative path to the file or directory
newName
stringThe new name of the file or directory
Returns
- bool
A successful rename flag
Unzip(string, string)
Attempts to unzip the specified zip file to the provided output directory. If the output path is not specified, the contents will be extracted to the same directory as the zip file. It will also overwrite any overlapping content.
public static bool Unzip(string zipPath, string outputPath)
Parameters
zipPath
stringThe full path to the zip file to unzip
outputPath
stringThe output path where the contents will be extracted
Returns
- bool
A successful unzip action
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
Zip(string, string)
Attempts to zip the file or directory at the specified path. If the output path is not specified, then the zip file will be created in the same directory as the source file, however, it will throw an exception. It will also overwrite the output if it already exists.
public static bool Zip(string path, string outputPath)
Parameters
path
stringThe full path to the file or directory to zip
outputPath
stringThe output path of the zip file
Returns
- bool
A successful zip action