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.

Copy
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.

Copy
EventHandler<ControllerFilesChangedEventArgs> Changed;

Example Code

Copy
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

Controller.Files Methods