File

The File object provides methods to access, read and write information from and to a file on disk. Using this object, it is possible to save and open settings across multiple sessions of your program.

Properties

LastError

File.LastError

Gets or sets the last encountered file operation based error message. This property is useful for finding out when some method fails to execute.

Operations

ReadContents

File.ReadContents(filePath)

Opens a file and reads the entire file's contents. This method will be fast for small files that are less than an MB in size, but will start to slow down and will be noticeable for files greater than 10MB.

filePath

The full path of the file to read. An example of a full path will be c:\temp\settings.data.

Returns

The entire contents of the file.

WriteContents

File.WriteContents(filePath, contents)

Opens a file and writes the specified contents into it, replacing the original contents with the new content.

filePath

The full path of the file to write to. An example of a full path will be c:\temp\settings.data.

contents

The contents to write into the specified file.

Returns

If the operation was successful, this will return "SUCCESS". Otherwise, it will return "FAILED".

ReadLine

File.ReadLine(filePath, lineNumber)

Opens the specified file and reads the contents at the specified line number.

filePath

The full path of the file to read from. An example of a full path will be c:\temp\settings.data.

lineNumber

The line number of the text to be read.

Returns

The text at the specified line of the specified file.

WriteLine

File.WriteLine(filePath, lineNumber, contents)

Opens the specified file and write the contents at the specified line number. This operation will overwrite any existing content at the specified line.

filePath

The full path of the file to read from. An example of a full path will be c:\temp\settings.data.

lineNumber

The line number of the text to write.

contents

The contents to write at the specified line of the specified file.

Returns

If the operation was successful, this will return "SUCCESS". Otherwise, it will return "FAILED".

InsertLine

File.InsertLine(filePath, lineNumber, contents)

Opens the specified file and inserts the contents at the specified line number. This operation will not overwrite any existing content at the specifid line.

filePath

The full path of the file to read from. An example of a full path will be c:\temp\settings.data.

lineNumber

The line number of the text to insert.

contents

The contents to insert into the file.

Returns

If the operation was successful, this will return "SUCCESS". Otherwise, it will return "FAILED".

AppendContents

File.AppendContents(filePath, contents)

Opens the specified file and appends the contents to the end of the file.

filePath

The full path of the file to read from. An example of a full path will be c:\temp\settings.data.

contents

The contents to append to the end of the file.

Returns

If the operation was successful, this will return "SUCCESS". Otherwise, it will return "FAILED".

CopyFile

File.CopyFile(sourceFilePath, destinationFilePath)

Copies the specified source file to the destination file path. If the destination points to a location that doesn't exist, the method will attempt to create it automatically. Existing files will be overwritten. It is always best to check if the destination file exists if you don't want to overwrite existing files.

sourceFilePath

The full path of the file that needs to be copied. An example of a full path will be c:\temp\settings.data.

destinationFilePath

The destination location or the file path.

Returns

If the operation was successful, this will return "SUCCESS". Otherwise, it will return "FAILED".

DeleteFile

File.DeleteFile(filePath)

Deletes the specified file.

filePath

The destination location or the file path. An example of a full path will be c:\temp\settings.data.

Returns

If the operation was successful, this will return "SUCCESS". Otherwise, it will return "FAILED".

CreateDirectory

File.CreateDirectory(directoryPath)

Creates the specified directory.

directoryPath

The full path of the directory to be created.

Returns

If the operation was successful, this will return "SUCCESS". Otherwise, it will return "FAILED".

DeleteDirectory

File.DeleteDirectory(directoryPath)

Deletes the specified directory.

directoryPath

The full path of the directory to be deleted.

Returns

If the operation was successful, this will return "SUCCESS". Otherwise, it will return "FAILED".

GetFiles

File.GetFiles(directoryPath)

Gets the path of all the files in the specified directory path.

directoryPath

The directory to look for files.

Returns

If the operation was successful, this will return the files as an array. Otherwise, it will return "FAILED".

GetDirectories

File.GetDirectories(directoryPath)

Gets the path of all the directories in the specified directory path.

directoryPath

The directory to look for subdirectories.

Returns

If the operation was successful, this will return the list of directories as an array. Otherwise, it will return "FAILED".

GetTemporaryFilePath

File.GetTemporaryFilePath()

Creates a new temporary file in a temporary directory and returns the full file path.

Returns

The full file path of the temporary file.

GetSettingsFilePath

File.GetSettingsFilePath()

Gets the full path of the settings file for this program. The settings file name is based on the program's name and is present in the same location as the program.

Returns

The full path of the settings file specific for this program.