SFTP file handling

SFTP (Secure File Transfer Protocol) is a protocol on SSH that allows to create, read, update, delete (CRUD) files and folders on the robot controller.

This feature is made possible because an embedded Linux is running in the robot.

You can also use this library to execute Linux command lines remotely, see the SSH documentation.

Connect to the robot via SFTP

SFTP features are not enabled by default when connecting to the robot. You must enable it and specify the Linux user name and password. If these credentials are not specified, the user is "ur" and the password "easybot".

To do this set the Ssh.EnableSftp property in ConnectParameters class.

Please then refer to the API reference below which lists all the available file and folder handling features.

If you wish to manipulate files outside of any instance of the UR object, you can create an instance of class Ssh.SftpClient which has the same functions.

// Create a new robot instance
var robot = new UR();
// Setup connection to th robot
var param = new ConnectParameters();
param.IP = "192.168.0.1";
// Enable SFTP
param.Ssh.EnableSftp = true;
// If not specified, credentials are "ur" with password "easybot"
param.Ssh.Username = "John";
param.Ssh.Password = "!!PASS!!";
// Connect to the robot
robot.Connect(param);
// Download a file from the robot
robot.Sftp.DownloadFile("/home/ur/ursim-current/programs/my-program.urp", @"C:\temp\my-program.urp");
// send file my-program.urp to the robot
robot.Sftp.UploadFile(@"C:\temp\my-program.urp", "/home/ur/ursim-current/programs/my-program.urp");
// Rename a file
robot.Sftp.RenameFile("/home/ur/ursim-current/programs/my-program.urp", "/home/ur/ursim-current/programs/my-program2.urp");
// Enumerate a directory
foreach (var element in robot.Sftp.ListDirectory("/home/ur/ursim-current/programs/"))
{
Console.WriteLine($"Name : {element.Name}");
Console.WriteLine($"IsDirectory : {element.IsDirectory}");
Console.WriteLine($"Bytes : {element.Length}");
Console.WriteLine($"Write time : {element.LastWriteTimeUtc}");
}
// Delete file or directory
robot.Sftp.Delete("/home/ur/ursim-current/programs/my-program.urp");
// Write in file
robot.Sftp.WriteAllText("/home/ur/ursim-current/programs/file.txt", "Hello !");

Try it with the Windows example

SFTP

API Reference

Members of Ssh.SftpClient :
public class SftpClient : SftpClientBase {
// Connects to the robot
public void Connect(string ip, string username, string password, int port = 22)
}
Members of Ssh.Internal.SftpClientBase :
public abstract class SftpClientBase : URServiceBase {
// Appends lines to a file, creating the file if it does not already exist.
public void AppendAllLines(string path, string[] contents)
// Appends lines to a file by using a specified encoding, creating the file if it does not already exist.
public void AppendAllLines(string path, string[] contents, Encoding encoding)
// Appends the specified string to the file, creating the file if it does not already exist.
public void AppendAllText(string path, string contents)
// Appends the specified string to the file, creating the file if it does not already exist.
public void AppendAllText(string path, string contents, Encoding encoding)
// Creates a <xref href="System.IO.StreamWriter" data-throw-if-not-resolved="false"></xref> that appends UTF-8 encoded text to the specified file,
// creating the file if it does not already exist.
public StreamWriter AppendText(string path)
// Creates a <xref href="System.IO.StreamWriter" data-throw-if-not-resolved="false"></xref> that appends text to a file using the specified
// encoding, creating the file if it does not already exist.
public StreamWriter AppendText(string path, Encoding encoding)
// Begins an asynchronous file downloading into the stream.
public IAsyncResult BeginDownloadFile(string path, Stream output)
// Begins an asynchronous file downloading into the stream.
public IAsyncResult BeginDownloadFile(string path, Stream output, AsyncCallback asyncCallback)
// Begins an asynchronous file downloading into the stream.
public IAsyncResult BeginDownloadFile(string path, Stream output, AsyncCallback asyncCallback, object state, Action<ulong> downloadCallback = null)
// Begins an asynchronous operation of retrieving list of files in remote directory.
public IAsyncResult BeginListDirectory(string path, AsyncCallback asyncCallback, object state, Action<int> listCallback = null)
// Begins the synchronize directories.
public IAsyncResult BeginSynchronizeDirectories(string sourcePath, string destinationPath, string searchPattern, AsyncCallback asyncCallback, object state)
// Begins an asynchronous uploading the stream into remote file.
public IAsyncResult BeginUploadFile(Stream input, string path)
// Begins an asynchronous uploading the stream into remote file.
public IAsyncResult BeginUploadFile(Stream input, string path, AsyncCallback asyncCallback)
// Begins an asynchronous uploading the stream into remote file.
public IAsyncResult BeginUploadFile(Stream input, string path, AsyncCallback asyncCallback, object state, Action<ulong> uploadCallback = null)
// Begins an asynchronous uploading the stream into remote file.
public IAsyncResult BeginUploadFile(Stream input, string path, bool canOverride, AsyncCallback asyncCallback, object state, Action<ulong> uploadCallback = null)
// Gets or sets the maximum size of the buffer in bytes.
public uint BufferSize { get; set; }
// Changes remote directory to path.
public void ChangeDirectory(string path)
// Changes permissions of file(s) to specified mode.
public void ChangePermissions(string path, short mode)
// Gets a value indicating if this client is connected to the robot
public bool Connected { get; }
protected void ConnectInternal(string ip, int port, string username, string password)
// Creates or overwrites a file in the specified path.
public SftpFileStream Create(string path)
// Creates or overwrites the specified file.
public SftpFileStream Create(string path, int bufferSize)
// Creates remote directory specified by path.
public void CreateDirectory(string path)
// Creates or opens a file for writing UTF-8 encoded text.
public StreamWriter CreateText(string path)
// Creates or opens a file for writing text using the specified encoding.
public StreamWriter CreateText(string path, Encoding encoding)
// Deletes the specified file or directory.
public void Delete(string path)
// Deletes remote directory specified by path.
public void DeleteDirectory(string path)
// Deletes remote file specified by path.
public void DeleteFile(string path)
public void Disconnect()
// Downloads remote file specified by the path into the stream.
public void DownloadFile(string path, Stream output, Action<ulong> downloadCallback = null)
// Downloads remote file specified by the path into the stream.
public void DownloadFile(string path, string localPath, Action<ulong> downloadCallback = null)
// Ends an asynchronous file downloading into the stream.
public void EndDownloadFile(IAsyncResult asyncResult)
// Ends an asynchronous operation of retrieving list of files in remote directory.
public SftpFile[] EndListDirectory(IAsyncResult asyncResult)
// Ends the synchronize directories.
public FileInfo[] EndSynchronizeDirectories(IAsyncResult asyncResult)
// Ends an asynchronous uploading the stream into remote file.
public void EndUploadFile(IAsyncResult asyncResult)
// Checks whether file or directory exists;
public bool Exists(string path)
// Gets reference to remote file or directory.
public SftpFile Get(string path)
// Gets the <xref href="UnderAutomation.UniversalRobots.Ssh.Tools.Sftp.SftpFileAttributes" data-throw-if-not-resolved="false"></xref> of the file on the path.
public SftpFileAttributes GetAttributes(string path)
// Returns the date and time the specified file or directory was last accessed.
public DateTime GetLastAccessTime(string path)
// Returns the date and time, in coordinated universal time (UTC), that the specified file or directory was last accessed.
public DateTime GetLastAccessTimeUtc(string path)
// Returns the date and time the specified file or directory was last written to.
public DateTime GetLastWriteTime(string path)
// Returns the date and time, in coordinated universal time (UTC), that the specified file or directory was last written to.
public DateTime GetLastWriteTimeUtc(string path)
// Gets status using [email protected] request.
public SftpFileSytemInformation GetStatus(string path)
// Retrieves list of files in remote directory.
public SftpFile[] ListDirectory(string path, Action<int> listCallback = null)
// Opens a <xref href="UnderAutomation.UniversalRobots.Ssh.Tools.Sftp.SftpFileStream" data-throw-if-not-resolved="false"></xref> on the specified path with read/write access.
public SftpFileStream Open(string path, FileMode mode)
// Opens a <xref href="UnderAutomation.UniversalRobots.Ssh.Tools.Sftp.SftpFileStream" data-throw-if-not-resolved="false"></xref> on the specified path, with the specified mode and access.
public SftpFileStream Open(string path, FileMode mode, FileAccess access)
// Opens an existing file for reading.
public SftpFileStream OpenRead(string path)
// Opens an existing UTF-8 encoded text file for reading.
public StreamReader OpenText(string path)
// Opens a file for writing.
public SftpFileStream OpenWrite(string path)
// Gets or sets the operation timeout.
public TimeSpan OperationTimeout { get; set; }
// Gets sftp protocol version.
public int ProtocolVersion { get; }
// Opens a binary file, reads the contents of the file into a byte array, and closes the file.
public byte[] ReadAllBytes(string path)
// Opens a text file, reads all lines of the file using UTF-8 encoding, and closes the file.
public string[] ReadAllLines(string path)
// Opens a file, reads all lines of the file with the specified encoding, and closes the file.
public string[] ReadAllLines(string path, Encoding encoding)
// Opens a text file, reads all lines of the file with the UTF-8 encoding, and closes the file.
public string ReadAllText(string path)
// Opens a file, reads all lines of the file with the specified encoding, and closes the file.
public string ReadAllText(string path, Encoding encoding)
// Reads the lines of a file with the UTF-8 encoding.
public string[] ReadLines(string path)
// Read the lines of a file that has a specified encoding.
public string[] ReadLines(string path, Encoding encoding)
// Renames remote file from old path to new path.
public void RenameFile(string oldPath, string newPath)
// Renames remote file from old path to new path.
public void RenameFile(string oldPath, string newPath, bool isPosix)
// Sets the specified <xref href="UnderAutomation.UniversalRobots.Ssh.Tools.Sftp.SftpFileAttributes" data-throw-if-not-resolved="false"></xref> of the file on the specified path.
public void SetAttributes(string path, SftpFileAttributes fileAttributes)
// Creates a symbolic link from old path to new path.
public void SymbolicLink(string path, string linkPath)
// Synchronizes the directories.
public FileInfo[] SynchronizeDirectories(string sourcePath, string destinationPath, string searchPattern)
// Uploads stream into remote file.
public void UploadFile(Stream input, string path, Action<ulong> uploadCallback = null)
// Uploads stream into remote file.
public void UploadFile(Stream input, string path, bool canOverride, Action<ulong> uploadCallback = null)
// Uploads file into remote file.
public void UploadFile(string localPath, string path, Action<ulong> uploadCallback = null)
// Gets remote working directory.
public string WorkingDirectory { get; }
// Writes the specified byte array to the specified file, and closes the file.
public void WriteAllBytes(string path, byte[] bytes)
// Writes a collection of strings to the file using the UTF-8 encoding, and closes the file.
public void WriteAllLines(string path, string[] contents)
// Writes the specified string to the file using the UTF-8 encoding, and closes the file.
public void WriteAllText(string path, string contents)
// Writes the specified string to the file using the specified encoding, and closes the file.
public void WriteAllText(string path, string contents, Encoding encoding)
}
Members of Ssh.Tools.Sftp.SftpFile :
public class SftpFile {
// Gets the file attributes.
public SftpFileAttributes Attributes { get; }
// Permanently deletes a file on remote machine.
public void Delete()
// Gets the full path of the directory or file.
public string FullName { get; }
// Gets or sets a value indicating whether the group members can execute this file.
public bool GroupCanExecute { get; set; }
// Gets or sets a value indicating whether the group members can read from this file.
public bool GroupCanRead { get; set; }
// Gets or sets a value indicating whether the group members can write into this file.
public bool GroupCanWrite { get; set; }
// Gets or sets file group id.
public int GroupId { get; set; }
// Gets a value indicating whether file represents a block device.
public bool IsBlockDevice { get; }
// Gets a value indicating whether file represents a character device.
public bool IsCharacterDevice { get; }
// Gets a value indicating whether file represents a directory.
public bool IsDirectory { get; }
// Gets a value indicating whether file represents a named pipe.
public bool IsNamedPipe { get; }
// Gets a value indicating whether file represents a regular file.
public bool IsRegularFile { get; }
// Gets a value indicating whether file represents a socket.
public bool IsSocket { get; }
// Gets a value indicating whether file represents a symbolic link.
public bool IsSymbolicLink { get; }
// Gets or sets the time the current file or directory was last accessed.
public DateTime LastAccessTime { get; set; }
// Gets or sets the time, in coordinated universal time (UTC), the current file or directory was last accessed.
public DateTime LastAccessTimeUtc { get; set; }
// Gets or sets the time when the current file or directory was last written to.
public DateTime LastWriteTime { get; set; }
// Gets or sets the time, in coordinated universal time (UTC), when the current file or directory was last written to.
public DateTime LastWriteTimeUtc { get; set; }
// Gets or sets the size, in bytes, of the current file.
public long Length { get; }
// Moves a specified file to a new location on remote machine, providing the option to specify a new file name.
public void MoveTo(string destFileName)
// For files, gets the name of the file. For directories, gets the name of the last directory in the hierarchy if a hierarchy exists.
// Otherwise, the Name property gets the name of the directory.
public string Name { get; }
// Gets or sets a value indicating whether the others can execute this file.
public bool OthersCanExecute { get; set; }
// Gets or sets a value indicating whether the others can read from this file.
public bool OthersCanRead { get; set; }
// Gets or sets a value indicating whether the others can write into this file.
public bool OthersCanWrite { get; set; }
// Gets or sets a value indicating whether the owner can execute this file.
public bool OwnerCanExecute { get; set; }
// Gets or sets a value indicating whether the owner can read from this file.
public bool OwnerCanRead { get; set; }
// Gets or sets a value indicating whether the owner can write into this file.
public bool OwnerCanWrite { get; set; }
// Sets file permissions.
public void SetPermissions(short mode)
// Returns a <xref href="System.String" data-throw-if-not-resolved="false"></xref> that represents this instance.
public override string ToString()
// Updates file status on the server.
public void UpdateStatus()
// Gets or sets file user id.
public int UserId { get; set; }
}
Members of Ssh.Tools.Sftp.SftpFileAttributes :
public class SftpFileAttributes {
// Gets or sets the extensions.
public IDictionary<string, string> Extensions { get; }
// Returns a byte array representing the current <xref href="UnderAutomation.UniversalRobots.Ssh.Tools.Sftp.SftpFileAttributes" data-throw-if-not-resolved="false"></xref>.
public byte[] GetBytes()
// Gets a value indicating whether the group members can execute this file.
public bool GroupCanExecute { get; set; }
// Gets a value indicating whether the group members can read from this file.
public bool GroupCanRead { get; set; }
// Gets a value indicating whether the group members can write into this file.
public bool GroupCanWrite { get; set; }
// Gets or sets file group id.
public int GroupId { get; set; }
// Gets a value indicating whether file represents a block device.
public bool IsBlockDevice { get; }
// Gets a value indicating whether file represents a character device.
public bool IsCharacterDevice { get; }
// Gets a value indicating whether file represents a directory.
public bool IsDirectory { get; }
// Gets a value indicating whether file represents a named pipe.
public bool IsNamedPipe { get; }
// Gets a value indicating whether file represents a regular file.
public bool IsRegularFile { get; }
// Gets a value indicating whether file represents a socket.
public bool IsSocket { get; }
// Gets a value indicating whether file represents a symbolic link.
public bool IsSymbolicLink { get; }
// Gets or sets the local time the current file or directory was last accessed.
public DateTime LastAccessTime { get; set; }
// Gets or sets the UTC time the current file or directory was last accessed.
public DateTime LastAccessTimeUtc { get; set; }
// Gets or sets the local time when the current file or directory was last written to.
public DateTime LastWriteTime { get; set; }
// Gets or sets the UTC time when the current file or directory was last written to.
public DateTime LastWriteTimeUtc { get; set; }
// Gets a value indicating whether the others can execute this file.
public bool OthersCanExecute { get; set; }
// Gets a value indicating whether the others can read from this file.
public bool OthersCanRead { get; set; }
// Gets a value indicating whether the others can write into this file.
public bool OthersCanWrite { get; set; }
// Gets a value indicating whether the owner can execute this file.
public bool OwnerCanExecute { get; set; }
// Gets a value indicating whether the owner can read from this file.
public bool OwnerCanRead { get; set; }
// Gets a value indicating whether the owner can write into this file.
public bool OwnerCanWrite { get; set; }
// Sets the permissions.
public void SetPermissions(short mode)
// Gets or sets the size, in bytes, of the current file.
public long Size { get; set; }
// Gets or sets file user id.
public int UserId { get; set; }
}

Read more