Class FileManager
Provides utility methods for managing files and directories within different storage locations.
Inheritance
Inherited Members
Namespace: UniUtils.Data
Assembly: cs.temp.dll.dll
Syntax
public static class FileManager
Methods
ClearDirectory(String, EStorageLocation)
Deletes all contents of a directory without deleting the directory itself.
Declaration
public static void ClearDirectory(string relativePath, EStorageLocation location = EStorageLocation.Persistent)
Parameters
Type | Name | Description |
---|---|---|
System.String | relativePath | The relative path of the directory to clear. |
EStorageLocation | location | The storage location. Defaults to Persistent. |
Examples
FileManager.ClearDirectory("Cache");
Exceptions
Type | Condition |
---|---|
System.IO.IOException | Thrown if file or directory deletion fails. |
System.UnauthorizedAccessException | Thrown if the operation lacks necessary permissions. |
CopyDirectory(String, String, EStorageLocation)
Copies the contents of a source directory to a target directory within a specified storage location.
Declaration
public static bool CopyDirectory(string sourceRelativePath, string targetRelativePath, EStorageLocation location = EStorageLocation.Persistent)
Parameters
Type | Name | Description |
---|---|---|
System.String | sourceRelativePath | The relative path of the source directory. |
System.String | targetRelativePath | The relative path of the target directory. |
EStorageLocation | location | The storage location to use. Defaults to Persistent. |
Returns
Type | Description |
---|---|
System.Boolean |
|
Examples
FileManager.CopyDirectory("Configs", "Backup/Configs");
Exceptions
Type | Condition |
---|---|
System.IO.DirectoryNotFoundException | Thrown if the source directory does not exist. |
System.IO.IOException | Thrown if copying fails due to IO errors. |
System.UnauthorizedAccessException | Thrown if the operation lacks necessary permissions. |
DeleteDirectory(String, EStorageLocation)
Deletes a specified directory within a given storage location.
Declaration
public static bool DeleteDirectory(string relativePath, EStorageLocation location = EStorageLocation.Persistent)
Parameters
Type | Name | Description |
---|---|---|
System.String | relativePath | The relative path of the directory to delete. |
EStorageLocation | location | The storage location to use for resolving the directory path. Defaults to Persistent. |
Returns
Type | Description |
---|---|
System.Boolean |
|
Exceptions
Type | Condition |
---|---|
System.IO.IOException | Thrown if file or directory deletion fails. |
System.UnauthorizedAccessException | Thrown if the operation lacks necessary permissions. |
EnsureDirectoryExists(String, EStorageLocation)
Ensures that a directory exists at the specified relative path within a given storage location. If the directory does not exist, it is created.
Declaration
public static void EnsureDirectoryExists(string relativePath, EStorageLocation location = EStorageLocation.Persistent)
Parameters
Type | Name | Description |
---|---|---|
System.String | relativePath | The relative path of the directory to check or create. |
EStorageLocation | location | The storage location to use for resolving the directory path. Defaults to Persistent. |
GetFilesInDirectory(String, EStorageLocation)
Retrieves a list of files from a specified directory within a given storage location.
Declaration
public static List<FileHandle> GetFilesInDirectory(string relativePath, EStorageLocation location = EStorageLocation.Persistent)
Parameters
Type | Name | Description |
---|---|---|
System.String | relativePath | The relative path to the directory from which to retrieve files. |
EStorageLocation | location | The storage location to use for resolving the directory path. Defaults to Persistent. |
Returns
Type | Description |
---|---|
List<FileHandle> | A list of FileHandle objects representing the files in the specified directory. If the directory does not exist, an empty list is returned. |
Examples
List<FileHandle> files = FileManager.GetFilesInDirectory("Logs");
foreach (FileHandle file in files)
file.Delete();
GetRootPath(EStorageLocation)
Retrieves the root path for the specified storage location.
Declaration
public static string GetRootPath(EStorageLocation location)
Parameters
Type | Name | Description |
---|---|---|
EStorageLocation | location | The storage location for which to retrieve the root path. |
Returns
Type | Description |
---|---|
System.String | The root path of the specified storage location. |
GetSubdirectories(String, EStorageLocation)
Retrieves a list of subdirectories from a specified directory within a given storage location.
Declaration
public static List<string> GetSubdirectories(string relativePath, EStorageLocation location = EStorageLocation.Persistent)
Parameters
Type | Name | Description |
---|---|---|
System.String | relativePath | The relative path to the directory from which to retrieve subdirectories. |
EStorageLocation | location | The storage location to use for resolving the directory path. Defaults to Persistent. |
Returns
Type | Description |
---|---|
List<System.String> | A list of relative paths representing the subdirectories in the specified directory. If the directory does not exist, an empty list is returned. |
Examples
List<string> subdirs = FileManager.GetSubdirectories("Projects");
foreach (string dir in subdirs)
Debug.Log(dir);
MoveDirectory(String, String, EStorageLocation)
Moves a directory from one path to another within a given storage location.
Declaration
public static bool MoveDirectory(string fromRelativePath, string toRelativePath, EStorageLocation location = EStorageLocation.Persistent)
Parameters
Type | Name | Description |
---|---|---|
System.String | fromRelativePath | The relative path of the source directory. |
System.String | toRelativePath | The relative path of the target directory. |
EStorageLocation | location | The storage location. Defaults to Persistent. |
Returns
Type | Description |
---|---|
System.Boolean |
|
Examples
FileManager.MoveDirectory("TempData", "Archived/TempData");
Exceptions
Type | Condition |
---|---|
System.IO.IOException | Thrown if the target directory already exists or if the move fails. |
TryIOAction(Action, String, Action<Exception, String>)
Executes an I/O action and handles any exceptions that occur during its execution.
Declaration
public static bool TryIOAction(Action action, string errorContext, Action<Exception, string> onError = null)
Parameters
Type | Name | Description |
---|---|---|
Action | action | The I/O action to execute. |
System.String | errorContext | A descriptive context for the error, used to provide additional information in the error message. |
Action<Exception, System.String> | onError | An optional callback to handle exceptions. The callback receives the exception and the formatted error message. |
Returns
Type | Description |
---|---|
System.Boolean |
|