Search Results for

    Show / Hide Table of Contents

    Class FileHandle

    Represents a file handle that provides methods for file operations such as reading, writing, and deleting files.

    Inheritance
    System.Object
    FileHandle
    Inherited Members
    System.Object.Equals(System.Object)
    System.Object.Equals(System.Object, System.Object)
    System.Object.ReferenceEquals(System.Object, System.Object)
    System.Object.GetHashCode()
    System.Object.GetType()
    System.Object.MemberwiseClone()
    Namespace: UniUtils.Data
    Assembly: cs.temp.dll.dll
    Syntax
    public class FileHandle

    Constructors

    FileHandle(String, EStorageLocation)

    Initializes a new instance of the FileHandle class with the specified relative path and storage location. Ensures the directory for the file exists.

    Declaration
    public FileHandle(string relativePath, EStorageLocation location = EStorageLocation.Persistent)
    Parameters
    Type Name Description
    System.String relativePath

    The relative path of the file.

    EStorageLocation location

    The storage location. Defaults to Persistent.

    Properties

    Exists

    Declaration
    public virtual bool Exists { get; }
    Property Value
    Type Description
    System.Boolean

    Returns true if the file exists; otherwise, false.

    Extension

    Declaration
    public string Extension { get; }
    Property Value
    Type Description
    System.String

    Returns the file extension.

    FileName

    Declaration
    public virtual string FileName { get; }
    Property Value
    Type Description
    System.String

    The name of the file without the directory path.

    FullPath

    Declaration
    public virtual string FullPath { get; }
    Property Value
    Type Description
    System.String

    The full path to the file, including the directory and file name.

    IsReadOnly

    Declaration
    public virtual bool IsReadOnly { get; }
    Property Value
    Type Description
    System.Boolean

    Returns true if the file is read-only; otherwise, false.

    Location

    Declaration
    public virtual EStorageLocation Location { get; }
    Property Value
    Type Description
    EStorageLocation

    The storage location of the file, such as Persistent.

    RelativePath

    Declaration
    public virtual string RelativePath { get; }
    Property Value
    Type Description
    System.String

    The relative path of the file from the root of the specified storage location.

    Methods

    AppendText(String)

    Appends the specified text content to the file.

    Declaration
    public virtual bool AppendText(string content)
    Parameters
    Type Name Description
    System.String content

    The text content to append to the file.

    Returns
    Type Description
    System.Boolean

    true if the operation was successful; otherwise, false.

    Examples
    FileHandle handle = new FileHandle("log.txt");
    handle.AppendText("Log entry\n");
    Exceptions
    Type Condition
    System.IO.IOException

    Thrown if appending to the file fails.

    Copy(String, Nullable<EStorageLocation>, Boolean)

    Copies the current file to a new location and returns a new FileHandle instance for the copied file.

    Declaration
    public virtual FileHandle Copy(string newRelativePath, EStorageLocation? location = null, bool canOverwrite = false)
    Parameters
    Type Name Description
    System.String newRelativePath

    The relative path for the new file location.

    System.Nullable<EStorageLocation> location

    The storage location for the new file. If null, the current file's location is used.

    System.Boolean canOverwrite

    If true, allows overwriting an existing file at the new location; otherwise, throws an error if the target file already exists.

    Returns
    Type Description
    FileHandle

    A new FileHandle instance representing the copied file, or null if the operation fails.

    Examples
    FileHandle file = new FileHandle("data/source.txt");
    FileHandle copiedFile = file.Copy("data/destination.txt", canOverwrite: true);
    Exceptions
    Type Condition
    System.ArgumentException

    Thrown if the new relative path is invalid.

    System.IO.FileNotFoundException

    Thrown if the file does not exist.

    System.IO.IOException

    Thrown if writing to the file fails.

    Delete()

    Deletes the current file.

    Declaration
    public virtual bool Delete()
    Returns
    Type Description
    System.Boolean

    true if the file was successfully deleted; otherwise, false.

    Examples
    FileHandle handle = new FileHandle("temp/data.txt");
    handle.Delete();
    Exceptions
    Type Condition
    System.IO.IOException

    Thrown if the delete operation fails.

    Dump()

    Logs the contents of the file to the Unity console.

    Declaration
    public void Dump()
    Examples
    FileHandle handle = new FileHandle("logs/output.txt");
    handle.Dump(); // Logs file contents to Unity console.

    FileSizeBytes()

    Declaration
    public virtual long FileSizeBytes()
    Returns
    Type Description
    System.Int64

    Returns the size of the file in bytes, or 0 if the file does not exist.

    LastModified()

    Declaration
    public virtual DateTime? LastModified()
    Returns
    Type Description
    System.Nullable<DateTime>

    Returns the last modified date and time of the file, or null if the file does not exist.

    Move(String, Nullable<EStorageLocation>, Boolean)

    Moves the current file to a new location and returns a new FileHandle instance for the moved file.

    Declaration
    public virtual FileHandle Move(string newRelativePath, EStorageLocation? location = null, bool canOverwrite = false)
    Parameters
    Type Name Description
    System.String newRelativePath

    The relative path for the new file location.

    System.Nullable<EStorageLocation> location
    System.Boolean canOverwrite

    If true, allows overwriting an existing file at the new location; otherwise, throws an error if the target file already exists.

    Returns
    Type Description
    FileHandle

    A new FileHandle instance representing the moved file, or null if the operation fails.

    Examples
    FileHandle file = new FileHandle("data/oldname.txt");
    FileHandle movedFile = file.Move("data/newname.txt", canOverwrite: true);
    Exceptions
    Type Condition
    System.IO.FileNotFoundException

    Thrown if the file does not exist.

    System.ArgumentException

    Thrown if the new relative path is invalid.

    System.IO.IOException

    Thrown if the target file already exists.

    System.IO.IOException

    Thrown if writing to the file fails.

    OpenReadStream()

    Opens and returns a read-only FileStream for this file.

    Declaration
    public virtual FileStream OpenReadStream()
    Returns
    Type Description
    FileStream

    A System.IO.FileStream opened for read.

    Examples
    FileHandle handle = new FileHandle("data.txt");
    using (FileStream stream = handle.OpenReadStream())
    {
        // Read from stream
    }
    Exceptions
    Type Condition
    System.IO.FileNotFoundException

    Thrown if the file does not exist.

    OpenWriteStream(Boolean)

    Opens and returns a write-only FileStream for this file.

    Declaration
    public virtual FileStream OpenWriteStream(bool overwrite = true)
    Parameters
    Type Name Description
    System.Boolean overwrite

    If true, the file will be recreated; if false, data will be appended.

    Returns
    Type Description
    FileStream

    A System.IO.FileStream opened for write.

    Examples
    FileHandle handle = new FileHandle("log.txt");
    using (FileStream stream = handle.OpenWriteStream(overwrite: true))
    {
        byte[] bytes = Encoding.UTF8.GetBytes("Hello");
        stream.Write(bytes, 0, bytes.Length);
    }

    ReadBytes()

    Reads the content of the file as a byte array.

    Declaration
    public virtual byte[] ReadBytes()
    Returns
    Type Description
    System.Byte[]

    The content of the file as a byte array, or null if the file does not exist.

    Examples
    FileHandle handle = new FileHandle("data.bin");
    byte[] data = handle.ReadBytes();
    Exceptions
    Type Condition
    System.IO.IOException

    Thrown if the file cannot be read.

    ReadText()

    Reads the content of the file as a string.

    Declaration
    public virtual string ReadText()
    Returns
    Type Description
    System.String

    The content of the file as a string, or null if the file does not exist.

    Examples
    FileHandle handle = new FileHandle("log.txt");
    string content = handle.ReadText();
    Debug.Log(content);
    Exceptions
    Type Condition
    System.IO.IOException

    Thrown if the file cannot be read.

    Rename(String, Boolean)

    Renames the current file by moving it to a new name within the same directory.

    Declaration
    public virtual FileHandle Rename(string newName, bool canOverwrite = false)
    Parameters
    Type Name Description
    System.String newName

    The new name for the file.

    System.Boolean canOverwrite

    If true, allows overwriting an existing file with the new name; otherwise, throws an error if the target file already exists.

    Returns
    Type Description
    FileHandle

    A new FileHandle instance representing the renamed file, or null if the operation fails.

    ToString()

    Returns the full path of the file as a string.

    Declaration
    public override string ToString()
    Returns
    Type Description
    System.String

    The full path of the file.

    Overrides
    System.Object.ToString()

    WriteBytes(Byte[])

    Writes the specified byte array content to the file, overwriting any existing content.

    Declaration
    public virtual bool WriteBytes(byte[] content)
    Parameters
    Type Name Description
    System.Byte[] content

    The byte array content to write to the file.

    Returns
    Type Description
    System.Boolean

    true if the operation was successful; otherwise, false.

    Examples
    FileHandle handle = new FileHandle("binary.bin");
    handle.WriteBytes(new byte[] { 1, 2, 3 });
    Exceptions
    Type Condition
    System.IO.IOException

    Thrown if writing to the file fails.

    WriteText(String)

    Writes the specified text content to the file, overwriting any existing content.

    Declaration
    public virtual bool WriteText(string content)
    Parameters
    Type Name Description
    System.String content

    The text content to write to the file.

    Returns
    Type Description
    System.Boolean

    true if the operation was successful; otherwise, false.

    Examples
    FileHandle handle = new FileHandle("output.txt");
    handle.WriteText("Hello, world!");
    Exceptions
    Type Condition
    System.IO.IOException

    Thrown if writing to the file fails.

    WriteText(String, Encoding)

    Writes the specified text content to the file using the provided encoding, overwriting any existing content.

    Declaration
    public virtual bool WriteText(string content, Encoding encoding)
    Parameters
    Type Name Description
    System.String content

    The text content to write to the file.

    Encoding encoding

    The encoding to use when writing the text content.

    Returns
    Type Description
    System.Boolean

    true if the operation was successful; otherwise, false.

    Examples
    FileHandle handle = new FileHandle("utf8.txt");
    handle.WriteText("Hello", Encoding.UTF8);
    Exceptions
    Type Condition
    System.IO.IOException

    Thrown if writing to the file fails.

    In This Article
    Back to top UniUtils Documentation