Snippet. Vala. Creating a File

This snippet shows how to create a new file in Vala. Before creating the file, it is checked if the file already exists.

var file = File.new_for_path("filename.txt");

if (file.query_exists() == false) {
    try {
        file.create(FileCreateFlags.NONE);
        if(file.query_exists() == true){
            // File successfully created;
        }
    } catch (Error e){
        // Error occurred
    }
} else {
    // File already exists
}

Updated on: 29 Mar 2024