-
Notifications
You must be signed in to change notification settings - Fork 1
FileHelper
FileHelper provides utility methods for common file system operations including path validation, file locking detection, directory management, application data paths, and path normalization.
| 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 |
Determines whether a path is a valid file path.
bool isValid = FileHelper.IsValidFilePath("myfile.txt"); // true
bool isValid2 = FileHelper.IsValidFilePath("invalid|path"); // falseDetermines 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.");
}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");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/MyGameThe folder is automatically created if it does not exist.
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);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"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"| 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 |
- MathHelper
- FileHelper
- HashHelper
- JsonHelper
- MapHelper
- TextHelper
- SoundHelper
- AlignHelpers
- Instance Helper
- Enum Extensions
- String Extensions
- Int Extensions
- Float Extensions
- IEnumerable Extensions
- Random Extensions
- Sound Extensions
- Font Extensions