minute read
Files
You are reading our Automation1 API documentation for the .NET programming language.
The Basics
In the .NET API, Files lets you read and write controller files on the Automation1 controller. Each Automation1 controller has its own file system to store controller files.
How to Use
Like any file system, you can read and write controller files, get a list of all the controller files, and delete controller files. When you read or write a controller file, you read or write all of its contents. You cannot partially open or modify a controller file. You can read or write controller files as string text or as byte arrays. To access one or more controller files, use the Controller.Files property. Refer to the example that follows.
byte[] contents = controller.Files.ReadBytes(string controllerFileName);
controller.Files.WriteBytes(string controllerFileName, byte[] bytes);
string contents = controller.Files.ReadText(string controllerFileName);
controller.Files.WriteText(string controllerFileName, string contents);
controller.Files.Delete(string controllerFileName);
string[] allFiles = controller.Files.GetFiles();
There is also an event on the Controller.Files property to which you can subscribe. It lets you know when a controller file changes or is deleted.
EventHandler<ControllerFilesChangedEventArgs> Changed;
Example Code
controller.Files.Changed += (sender, args) =>
{
Console.WriteLine("A file changed!");
};
controller.Files.WriteText("MyExampleFile.txt", "Hello world!");
string fileContents = controller.Files.ReadText("MyExampleFile.txt");
Console.WriteLine(fileContents);
controller.Files.Delete("MyExampleFile.txt");
Thread Safety
All the methods and properties that operate under the Controller.Files property are thread safe. You can call them from two or more threads without interference.
Full Reference
For more information about the events and methods that are available for Files, refer to the lists that follow.
Controller.Files Events
Raised when a controller file(s) changed on the Automation1 controller.
Controller.Files Methods
Deletes a controller file on the Automation1 controller.
controllerFileName: The file to delete.
Returns the names of all controller files (including their paths) on the Automation1 controller.
Returns: An array of names of all controller files (including their paths), or an empty array if no files exist.
Returns the contents of a controller file from the Automation1 controller as raw bytes.
controllerFileName: The file to read from.
Returns: The byte array of file contents.
Returns the contents of a controller file from the Automation1 controller as UTF-8 encoded text.
controllerFileName: The file to read from.
Returns: The UTF-8 encoded text file contents.
Returns the contents of a controller file from the Automation1 controller as text with the specified encoding.
controllerFileName: The file to read from.
encoding: The text encoding to use.
Returns: The text file contents encoded with encoding.
Creates a new controller file on the Automation1 controller and writes the specified contents to it. This method will overwrite any existing file that has the same file name.
controllerFileName: The file to write to.
bytes: The byte array of a file's contents to write.
Creates a new controller file on the Automation1 controller and writes the specified contents to it as UTF-8 encoded text (byte-order mark not included). This method will overwrite any existing file that has the same file name.
controllerFileName: The file to write to.
contents: The UTF-8 encoded text file contents to write (byte-order mark not included).
Creates a new controller file on the Automation1 controller and writes the specified contents to it as text with the specified encoding. This method will overwrite any existing file that has the same file name.
controllerFileName: The file to write to.
contents: The text file contents to write encoded with encoding.
encoding: The text encoding to use.