File and directory functions

Energy.Base.File class is a collection of file and directory utility functions.

MakeDirectory

Create directory if not exists. Returns true if a directory exists or has been created.

bool Energy.Base.File.MakeDirectory(string path)

RemoveDirectory

Remove directory if exists. Returns true if directory has been removed or not exists.

bool Energy.Base.File.RemoveDirectory(string path, bool recursive)

Remove directory if exists and is empty.

bool Energy.Base.File.RemoveDirectory(string path)

GetBaseDirectory

Gets the base directory that the assembly resolver uses to probe for assemblies.

string Energy.Base.File.GetBaseDirectory()

GetBasePath

Gets the base directory that the assembly resolver uses to probe for assemblies. Return path with trailing directory separator.

string Energy.Base.File.GetBasePath()

Locate

Locate file or executable in directories from PATH environment variable.

If file can’t be found, empty string will be returned.

string Energy.Base.File.Locate(string command)

Locate file or executable in search directories.

string Energy.Base.File.Locate(string command, string[] search)

Locate file with one of possible extensions in search directories.

string Energy.Base.File.Locate(string file, string[] search, string[] extension)

Locate file with one of possible extensions in search directories.

This version of function allows to specify lookup behaviour (iterate over directories or extensions).

string Energy.Base.File.Locate(string file, string[] search, string[] extension, Energy.Enumeration.LocateBehaviour behaviour)

Locate file with one of possible extensions in search directory.

This version of function allows to specify lookup behaviour (iterate over directories or extensions).

string Energy.Base.File.Locate(string[] list, string[] search, string[] extension, Energy.Enumeration.LocateBehaviour behaviour)

GetHomeDirectory

Get absolute path of home directory for current user.

Resulting path will include trailing directory separator.

string Energy.Base.File.GetHomeDirectory()

State

Energy.Base.File.State keeps cached metadata about a tracked file and exposes helper methods for detecting changes and managing the file.

State.Refresh

Refreshes the cached modification timestamp and size for the tracked file. Returns true when metadata was refreshed; returns false when the path is empty, the file is missing, or reading metadata raises an error.

bool Energy.Base.File.State.Refresh()

State.Exists

Checks whether the tracked file currently exists on disk.

bool Energy.Base.File.State.Exists()

State.IsChanged (tracked file)

Compares the cached timestamp and size with the current file metadata and returns true when the file changed or disappeared since the last refresh.

bool Energy.Base.File.State.IsChanged()

State.IsChanged (path)

Returns true when the supplied path differs from the cached path or when the tracked file metadata has changed.

bool Energy.Base.File.State.IsChanged(string file)

State.GetCreateStamp

Returns the file creation timestamp or DateTime.MinValue when the path is empty, the file is missing, or retrieving metadata fails.

DateTime Energy.Base.File.State.GetCreateStamp()

State.GetWriteStamp

Returns the last write timestamp for the tracked file or DateTime.MinValue when the path is empty, missing, or cannot be read.

DateTime Energy.Base.File.State.GetWriteStamp()

State.SetWriteStamp (explicit timestamp)

Sets the tracked file’s last write timestamp to the supplied value (or to the current time when DateTime.MinValue is provided). Returns true when the timestamp was updated.

bool Energy.Base.File.State.SetWriteStamp(DateTime now)

State.SetWriteStamp (current time)

Convenience overload that sets the last write timestamp to the current time.

bool Energy.Base.File.State.SetWriteStamp()

State.GetSize

Returns the file size in bytes or -1 when the file path is empty, does not exist, or cannot be read.

long Energy.Base.File.State.GetSize()

State.Clone

Creates a shallow copy of the current State, preserving cached path, timestamp, and size values.

object Energy.Base.File.State.Clone()

State.CreateFile

Creates the tracked file when it does not already exist, updates the cached timestamp/size, and returns true on success.

bool Energy.Base.File.State.CreateFile()

State.DeleteFile

Deletes the tracked file if present, resets cached metadata, and returns true even when the file was already missing.

bool Energy.Base.File.State.DeleteFile()

State.Touch

Updates the last write timestamp when the file exists or creates a new empty file otherwise.

bool Energy.Base.File.State.Touch()

GetName

Returns the file name portion of a path without leading directories.

string Energy.Base.File.GetName(string path)

GetExtension

Returns the filename extension (including the dot) or an empty string when none is present.

string Energy.Base.File.GetExtension(string path)

GetRoot

Returns the root component of a path (drive letter, UNC prefix, or leading separator) or an empty string if no root is present.

string Energy.Base.File.GetRoot(string path)

IncludeRoot

Ensures that a path contains a leading root component by prepending the provided root when necessary.

string Energy.Base.File.IncludeRoot(string path, string root)

ExcludeRoot

Removes the root component from the supplied path.

string Energy.Base.File.ExcludeRoot(string path)

IncludeSeparator

Appends a trailing directory separator to a path when one is missing.

string Energy.Base.File.IncludeSeparator(string path)

ExcludeSeparator

Removes a trailing directory separator from a path when present.

string Energy.Base.File.ExcludeSeparator(string path)

FileUniqueIdentity

Generates a unique file path within a directory by appending numeric suffixes to the base file name. When reserve is true, an empty file is created immediately to reserve the name. Each overload returns the full path to the unique file or null when the directory is invalid or reservation fails.

string Energy.Base.File.FileUniqueIdentity(string file, string path, bool reserve)
string Energy.Base.File.FileUniqueIdentity(string file, string path)
string Energy.Base.File.FileUniqueIdentity(string file, bool reserve)
string Energy.Base.File.FileUniqueIdentity(string file)

HasNoExtension

Returns true when the supplied file name does not contain a meaningful extension.

bool Energy.Base.File.HasNoExtension(string file)

IsDirectory

Returns true when the supplied path refers to an existing directory. The method falls back to suffix inspection on Compact Framework targets.

bool Energy.Base.File.IsDirectory(string file)

GetAbsolutePath

Converts a relative path into an absolute one by combining it with the current working directory (or a provided directory). Absolute paths are returned unchanged.

string Energy.Base.File.GetAbsolutePath(string file)
string Energy.Base.File.GetAbsolutePath(string file, string current)

IsRelativePath

Determines whether a path is relative. You can optionally supply a list of separator strings to treat as rooted prefixes.

bool Energy.Base.File.IsRelativePath(string path, string[] separator)
bool Energy.Base.File.IsRelativePath(string path)

DeleteFile

Deletes the specified file and returns an integer status code: 1 on success, 0 when the file was missing, -2 for I/O errors, -3 for access violations, and -1 for other failures.

int Energy.Base.File.DeleteFile(string file)

AppendText

Appends text to a file using the platform’s default encoding (when supported) and returns true on success or false if writing fails.

bool Energy.Base.File.AppendText(string file, string content)

ReadText

Reads the entire contents of a text file and returns the string or null when the file is missing or cannot be read. An overload lets you specify the encoding.

string Energy.Base.File.ReadText(string fileName)
string Energy.Base.File.ReadText(string fileName, Encoding encoding)