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:

    1. Directory Verification: Checks if the target directory exists; if not, it attempts to create it.
    2. File Stream: Attempts to open or create the file at the provided path.
    3. Serialization: Writes the provided content string to the file, overwriting any existing data.
    4. 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 CanleyCSVManager methods, this utility requires a full absolute path and does not default to the Unity persistentDataPath. Ensure your pathing logic is cross-platform compatible if targeting mobile or console.