-
Notifications
You must be signed in to change notification settings - Fork 0
Class File
Kristian Virtanen edited this page Oct 14, 2024
·
5 revisions
File class provides a set of methods for performing common file and directory operations.
File.ReadContents(filePath)- Reads the entire contents of a file.
- Returns the contents of the file as a string, or an empty string if an error occurs.
File.WriteContents(filepath, contents)- Writes the specified contents to a file, overwriting the file if it exists.
- True if the operation succeeds, otherwise false.
File.ReadLine(filePath, lineNumber)- Reads a specific line from a file.
- Returns the line contents as a string, or an empty string if an error occurs.
File.WriteLine(filePath, lineNumber, contents)- Writes a specific line in the file.
- True if the operation succeeds, otherwise false.
File.InsertLine(filePath, lineNumber, contents)- Inserts a specific line in the file without overwriting existing lines.
- Returns true if the operation succeeds, otherwise false.
File.AppendContents(filePath, contents)- Appends contents to the end of a file.
- Returns true if the operation succeeds, otherwise false.
File.CopyFile(sourceFilePath, destinationFilePath)- Copies a file from the source path to the destination path.
- Returns true if the operation succeeds, otherwise false.
File.DeleteFile(filePath)- Deletes the specified file.
- Returns true if the operation succeeds, otherwise false.
File.CreateDirectory(directoryPath)- Creates a new directory at the specified path.
- Returns true if the operation succeeds, otherwise false.
File.DeleteDirectory(directoryPath)- Deletes the specified directory and all its contents.
- Returns true if the operation succeeds, otherwise false.
File.GetFiles(directoryPath)- Retrieves all file paths in the specified directory.
- Returns a string of file paths seperated by line change, or an empty string if an error occurs.
File.GetDirectories(directoryPath)- Retrieves all directory paths in the specified directory.
- Returns a string of directory paths seperated by line change, or an empty string if an error occurs.
filePath = "testFile.txt"
directoryPath = "testDirectory"
copiedFilePath = "copiedTestFile.txt"
' Write contents to a file
TextWindow.WriteLine("Writing contents to file...")
File.WriteContents(filePath, "Hello, World!")
' Read contents from a file
TextWindow.WriteLine("Reading contents from file:")
TextWindow.WriteLine(SB.File.ReadContents(filePath))
' Append contents to a file
TextWindow.WriteLine("Appending contents to file...")
File.AppendContents(filePath, "Appending some text...")
' Read appended contents
TextWindow.WriteLine("Reading appended contents from file:")
TextWindow.WriteLine(SB.File.ReadContents(filePath))
' Insert a line into the file
TextWindow.WriteLine("Inserting a line into the file at line 2...")
File.InsertLine(filePath, 2, "Inserted line.")
' Read specific line
TextWindow.WriteLine("Reading line 2 from file:")
TextWindow.WriteLine(SB.File.ReadLine(filePath, 2))
' Copy the file
TextWindow.WriteLine("Copying file to another path...")
File.CopyFile(filePath, copiedFilePath)
' Read the copied file
TextWindow.WriteLine("Reading contents from copied file:")
TextWindow.WriteLine(SB.File.ReadContents(copiedFilePath))
' Create a directory
TextWindow.WriteLine("Creating a directory...")
File.CreateDirectory(directoryPath)
' Get files in the directory
TextWindow.WriteLine("Getting files in the directory:")
TextWindow.WriteLine(SB.File.GetFiles(directoryPath))
' Delete the directory
TextWindow.WriteLine("Deleting the directory...")
File.DeleteDirectory(directoryPath)
' Delete the files
TextWindow.WriteLine("Deleting original and copied files...")
File.DeleteFile(filePath)
File.DeleteFile(copiedFilePath)
TextWindow.WriteLine("All operations completed.")// Create an alias for the SmallBasicOpenEditionDll namespace
using SB = SmallBasicOpenEditionDll;
static class SB_Program
{
static void Main()
{
string filePath = "testFile.txt";
string directoryPath = "testDirectory";
string copiedFilePath = "copiedTestFile.txt";
// Write contents to a file
SB.TextWindow.WriteLine("Writing contents to file...");
SB.File.WriteContents(filePath, "Hello, World!");
// Read contents from a file
SB.TextWindow.WriteLine("Reading contents from file:");
SB.TextWindow.WriteLine(SB.File.ReadContents(filePath));
// Append contents to a file
SB.TextWindow.WriteLine("Appending contents to file...");
SB.File.AppendContents(filePath, "Appending some text...");
// Read appended contents
SB.TextWindow.WriteLine("Reading appended contents from file:");
SB.TextWindow.WriteLine(SB.File.ReadContents(filePath));
// Insert a line into the file
SB.TextWindow.WriteLine("Inserting a line into the file at line 2...");
SB.File.InsertLine(filePath, 2, "Inserted line.");
// Read specific line
SB.TextWindow.WriteLine("Reading line 2 from file:");
SB.TextWindow.WriteLine(SB.File.ReadLine(filePath, 2));
// Copy the file
SB.TextWindow.WriteLine("Copying file to another path...");
SB.File.CopyFile(filePath, copiedFilePath);
// Read the copied file
SB.TextWindow.WriteLine("Reading contents from copied file:");
SB.TextWindow.WriteLine(SB.File.ReadContents(copiedFilePath));
// Create a directory
SB.TextWindow.WriteLine("Creating a directory...");
SB.File.CreateDirectory(directoryPath);
// Get files in the directory
SB.TextWindow.WriteLine("Getting files in the directory:");
SB.TextWindow.WriteLine(SB.File.GetFiles(directoryPath));
// Delete the directory
SB.TextWindow.WriteLine("Deleting the directory...");
SB.File.DeleteDirectory(directoryPath);
// Delete the files
SB.TextWindow.WriteLine("Deleting original and copied files...");
SB.File.DeleteFile(filePath);
SB.File.DeleteFile(copiedFilePath);
SB.TextWindow.WriteLine("All operations completed.");
}
}