Skip to content

FileHelper

Shmellyorc edited this page Aug 31, 2026 · 2 revisions

FileHelper provides utility methods for common file system operations including path validation, file locking detection, directory management, application data paths, and path normalization.


Overview

Feature Description
Path Validation Check if a path is valid
File Locking Detect if a file is in use by another process
Directory Management Ensure directories exist
Application Data Get platform-specific app data paths
Path Normalization Normalize paths to a consistent format
LDtk Remapping Remap LDtk paths to logical paths

Path Validation

IsValidFilePath

Determines whether a path is a valid file path.

bool isValid = FileHelper.IsValidFilePath("myfile.txt");  // true
bool isValid2 = FileHelper.IsValidFilePath("invalid|path");  // false

File Locking

IsFileInUse

Determines whether a file is currently locked or in use by another process.

if (FileHelper.IsFileInUse("save.dat"))
{
    Console.WriteLine("Save file is locked by another process.");
}
else
{
    Console.WriteLine("File is available.");
}

Directory Management

EnsureDirectoryExists

Ensures that the directory for the specified path exists, creating it if necessary.

// Ensure a directory exists
bool created = FileHelper.EnsureDirectoryExists("MyFolder/SubFolder/");

// Ensure the directory for a file exists
bool created = FileHelper.EnsureDirectoryExists("MyFolder/SubFolder/file.txt");

Application Data

GetApplicationData

Gets the platform-specific application data folder path for the specified company and application.

string appData = FileHelper.GetApplicationData("MyCompany", "MyGame");

// Windows: %APPDATA%/MyCompany/MyGame
// macOS: ~/Library/Application Support/MyCompany/MyGame
// Linux: $XDG_CONFIG_HOME/MyCompany/MyGame or ~/.config/MyCompany/MyGame

The folder is automatically created if it does not exist.

Using Application Folders

The engine provides dedicated folders for different purposes through the Game instance.

// Save a temp file
string tempPath = Path.Combine(Game.Instance.ApplicationTempFolder, "temp_export.dat");
File.WriteAllBytes(tempPath, data);

// Save config
string configPath = Path.Combine(Game.Instance.ApplicationConfigFolder, "settings.json");
File.WriteAllText(configPath, jsonData);

// Save game data
string savePath = Path.Combine(Game.Instance.ApplicationSaveFolder, "save.dat");
File.WriteAllBytes(savePath, saveData);

Path Normalization

Normalize

Normalizes a file path by converting separators, removing empty segments, and resolving "." and ".." segments.

string normalized = FileHelper.Normalize("folder\\subfolder/../file.txt");
// Returns: "folder/file.txt"

string normalized2 = FileHelper.Normalize("/folder/./subfolder/../file.txt");
// Returns: "/folder/file.txt"

string normalized3 = FileHelper.Normalize("C:\\folder\\file.txt");
// Returns: "C:/folder/file.txt"

LDtk Remapping

RemapLDtkPath

Remaps an LDtk path to a logical path relative to the content root.

string remapped = FileHelper.RemapLDtkPath("Content/textures/player.png", "Content");
// Returns: "textures/player.png"

string remapped2 = FileHelper.RemapLDtkPath("textures/player.png", "Content");
// Returns: "textures/player.png"

Summary

Method Description
IsValidFilePath Checks if a path is valid
IsFileInUse Checks if a file is locked
EnsureDirectoryExists Creates directories if they don't exist
GetApplicationData Gets platform-specific app data path
Normalize Normalizes a file path
RemapLDtkPath Remaps LDtk paths to logical paths

Back to Home

Clone this wiki locally