Detailed Method Reference
Namespace: Canley.Utility.CSV
Class: CanleyCSVWriter
WriteToFile
public static bool WriteToFile(string path, string content)-
Description: A static power-user utility to write a string to a specific absolute path on the disk. This bypasses any internal path management and gives you direct control over the destination.
-
Workflow:
- Directory Verification: Checks if the target directory exists; if not, it attempts to create it.
- File Stream: Attempts to open or create the file at the provided
path. - Serialization: Writes the provided
contentstring to the file, overwriting any existing data. - Safety Check: Wraps the operation in a try-catch block to handle IO exceptions.
-
Parameters:
path: The absolute file path (e.g.,"C:/Exports/Data.csv").content: The raw string data to be written.
-
Returns:
bool(True if the write succeeded; False if the path is invalid, permissions are denied, or the file is locked). -
Example:
string csvString = "Header1,Header2\nValue1,Value2";
if (CanleyCSVWriter.WriteToFile("C:/Exports/Data.csv", csvString))
{
Debug.Log("File successfully written to disk.");
}
else
{
Debug.LogError("Failed to write file. Check path permissions or file locks.");
}WARNING
Unlike the
CanleyCSVManagermethods, this utility requires a full absolute path and does not default to the UnitypersistentDataPath. Ensure your pathing logic is cross-platform compatible if targeting mobile or console.