List and transfer files
List the files of a Staubli controller, upload and download files, create, rename and delete files and folders, with synchronous and asynchronous methods.
This page shows how to list, upload, download, rename and delete the files of a Staubli CS8 or CS9 controller from a PC, with controller.File. The same code works on a real controller (FTP) and on the emulator of Staubli Robotics Suite (folder of its .controller file): see Files overview for the connection.
List the files
GetListing(path) returns the files and folders of a folder. GetFileInfo(path) returns one item, or null when it does not exist.
var controller = new StaubliController();controller.Connect(parameters);// Content of a folder of the controllerforeach (FileItem item in controller.File.GetListing("/usr/usrapp"))Console.WriteLine($"{item.FullName} {item.Type} {item.Size} bytes {item.Modified}");// One file or folder, null when it does not existFileItem project = controller.File.GetFileInfo("/usr/usrapp/myApp/myApp.pjx");bool fileExists = controller.File.FileExists("/usr/usrapp/myApp/myApp.pjx");bool folderExists = controller.File.DirectoryExists("/usr/usrapp/myApp");controller.Disconnect();}
| Property | Meaning |
|---|---|
Name | Name, without the path |
FullName | Full path on the controller, for example /usr/usrapp/myApp/myApp.pjx |
Type | FileItemType.File or FileItemType.Directory |
Size | Size in bytes, 0 for a folder |
Modified | Date of the last change. With the emulator, local time of the PC |
Upload and download
var controller = new StaubliController();controller.Connect(parameters);// Upload a local file. createRemoteDir: true creates the folder on the controller when it does not exist.controller.File.UploadFileToController(@"C:\Data\points.dat", "/usr/usrapp/myApp/points.dat", true,progress => Console.WriteLine($"{progress:0}%"));// Upload bytescontroller.File.UploadBytesToController(Encoding.ASCII.GetBytes("1;2;3"), "/usr/usrapp/myApp/data.txt");// Download to a local file (replaced when it exists)controller.File.DownloadFileFromController(@"C:\Backup\myApp.pjx", "/usr/usrapp/myApp/myApp.pjx");// Download the contentbyte[] content = controller.File.DownloadBytesFromController("/usr/usrapp/myApp/data.txt");Console.WriteLine(Encoding.ASCII.GetString(content));controller.Disconnect();}
Upload
UploadFileToController(localPath, remotePath)sends a local file.UploadBytesToController(data, remotePath)andUploadStreamToController(stream, remotePath)send data from memory or a stream.- The file of the controller is replaced when it exists. With
createRemoteDirset totrue, its folder is created when it does not exist.
Download
DownloadFileFromController(localPath, remotePath)writes a local file. The local file is replaced, and its folder is created.DownloadBytesFromController(remotePath)returns the content,DownloadStreamFromController(stream, remotePath)writes it to a stream.- A file that does not exist on the controller throws a
FileException. No local file is created.
The optional progress argument receives the percentage of the transfer, from 0 to 100.
Folders, rename and delete
var controller = new StaubliController();controller.Connect(parameters);// Create a folder, with its parent folderscontroller.File.CreateDirectory("/usr/usrapp/myApp/backup");// Rename or move a file or a foldercontroller.File.Rename("/usr/usrapp/myApp/data.txt", "/usr/usrapp/myApp/backup/data.txt");// Delete a filecontroller.File.DeleteFile("/usr/usrapp/myApp/backup/data.txt");// Delete a folder and all its contentcontroller.File.DeleteDirectory("/usr/usrapp/myApp/backup");controller.Disconnect();}
CreateDirectorycreates the parent folders too. Nothing is done when the folder exists.Renamerenames or moves a file or a folder.DeleteFileandDeleteDirectorythrow aFileExceptionwhen the item does not exist.DeleteDirectorydeletes all the content.
To send a complete VAL 3 application, see Send a VAL 3 application.
Asynchronous methods
Each method has an asynchronous version, with an optional CancellationToken: GetListingAsync, UploadFileToControllerAsync, DownloadBytesFromControllerAsync... They are not available on .NET Framework 3.5 and 4.0.
var controller = new StaubliController();controller.Connect(parameters);using var cancellation = new CancellationTokenSource(TimeSpan.FromMinutes(1));FileItem[] items = await controller.File.GetListingAsync("/usr/usrapp", cancellation.Token);await controller.File.UploadFileToControllerAsync(@"C:\Data\points.dat", "/usr/usrapp/myApp/points.dat",cancellationToken: cancellation.Token);byte[] content = await controller.File.DownloadBytesFromControllerAsync("/usr/usrapp/myApp/points.dat",cancellationToken: cancellation.Token);controller.Disconnect();}
Reference
A file or a folder of the controller
| Member | Type | Description |
|---|---|---|
FullName Property read only | string | Full path of the file or folder on the controller (for example "/usr/usrapp/myApp/myApp.pjx") |
Modified Property read only | DateTime | Date and time of the last modification, as given by the controller. With a controller emulated by Staubli Robotics Suite, local time of the computer. |
Name Property read only | string | Name of the file or folder, without its path (for example "myApp.pjx") |
Size Property read only | long | Size of the file in bytes. 0 for a folder, and 0 when the controller does not give the size. |
Type Property read only | FileItemType | File or folder |
ToString() Method | string |