UnderAutomation
Une question ?

[email protected]

Contactez-nous
UnderAutomation
⌘Q
Cette page est disponible uniquement en anglais.

SFTP file handling

Download, upload, list, rename and delete the files of a UR controller with SFTP: programs, installations and any other file.

  • Prerequisites
  • Example
  • List the programs
  • Operations
  • Try it in the demo application
  • Reference
  • What to read next

SFTP (SSH File Transfer Protocol) gives access to the files of a Universal Robots controller: download, upload, list, rename and delete programs, installations and any other file. This page shows how to connect and use the SFTP client of the SDK. The controller runs Linux: the same connection can also run commands, see SSH.

Prerequisites

  • Secure Shell is enabled on the robot. On PolyScope, open Settings, Security, Secure Shell.
  • The Linux user and its password. The SDK uses ur and easybot by default.
TargetUserPasswordFolder of the programs
Robotrooteasybot/programs
URSimureasybot/home/ur/ursim-current/programs

Change the default password of the robot: anyone on the network who knows it can change its files.

Example

SFTP is disabled by default: set Ssh.EnableSftp in ConnectParameters.

static void Main(string[] args)
{
var robot = new UR();
var parameters = new ConnectParameters("192.168.0.1");
// SFTP is disabled by default
parameters.Ssh.EnableSftp = true;
// "ur" and "easybot" by default
parameters.Ssh.Username = "root";
parameters.Ssh.Password = "easybot";
robot.Connect(parameters);
// Download a program from the robot
robot.Sftp.DownloadFile("/programs/my_program.urp", @"C:\temp\my_program.urp");
// Send a program to the robot
robot.Sftp.UploadFile(@"C:\temp\my_program.urp", "/programs/my_program.urp");
// Rename, check, delete
robot.Sftp.RenameFile("/programs/my_program.urp", "/programs/old_program.urp");
bool exists = robot.Sftp.Exists("/programs/old_program.urp");
robot.Sftp.DeleteFile("/programs/old_program.urp");
// List a folder
foreach (SftpFile item in robot.Sftp.ListDirectory("/programs/"))
Console.WriteLine(item.Name + " " + item.IsDirectory + " " + item.Length + " " + item.LastWriteTimeUtc);
// Read and write text files
robot.Sftp.WriteAllText("/programs/note.txt", "Hello");
string text = robot.Sftp.ReadAllText("/programs/note.txt");
}
}
Click to see the full code

The progress callbacks of DownloadFile, UploadFile and ListDirectory are optional. In Python, pass a function that takes the number of bytes or of entries.

List the programs

EnumeratePrograms and EnumerateInstallations return the relative paths of the .urp and .installation files of the programs folder and of its subfolders. They look in /programs, or in the folder of URSim when /programs does not exist. The client also works without UR:

static void Main(string[] args)
{
// An SFTP client, without a UR instance
var client = new SftpClient();
client.Connect("192.168.0.1", "root", "easybot");
// Relative paths of the .urp files of the programs folder, and of its subfolders
string[] programs = client.EnumeratePrograms();
string[] installations = client.EnumerateInstallations();
client.Disconnect();
}
}
Click to see the full code

Operations

TaskMethods
Transfer a fileDownloadFile, UploadFile, with a local path or a Stream
Read and write a fileReadAllText, ReadAllLines, ReadAllBytes, WriteAllText, WriteAllLines, WriteAllBytes, AppendAllText, OpenRead, OpenWrite
BrowseListDirectory, Exists, Get, GetAttributes, ChangeDirectory, WorkingDirectory
ChangeCreateDirectory, RenameFile, DeleteFile, DeleteDirectory, Delete, ChangePermissions
DatesGetLastWriteTime, GetLastAccessTime, and their UTC versions

A program sent with SFTP is loaded with the Dashboard Server (LoadProgram) or the REST API. See Transfer files and backups.

Try it in the demo application

Everything on this page can be tried without writing code, in the File handling (SFTP) page of the demo application.

File handling (SFTP) page of the Universal Robots SDK demo application

The demo application is open source. The C# source of this page is SftpControl.cs.

Reference

Class
SftpClientinherits SftpClientBase
C#Python

Provides a client for transferring files to and from the Universal Robots controller using the SFTP protocol.

MemberTypeDescription
SftpClient()
Constructor
Connect(string, string, string, int)
Method
void
Connects to the robot
  • ip : Robot IP address
  • username : Robot linux username (default username is ur for simulator and root for real robot)
  • password : Associated user password (default is easybot)
  • port : SFTP port
Class
SftpClientBaseinherits URServiceBase
C#Python

Implementation of the SSH File Transfer Protocol (SFTP) over SSH for transfering files to the robot controller

MemberTypeDescription
SftpClientBase()
Constructor
BufferSize
Property
uint
Gets or sets the maximum size of the buffer in bytes.
Connected
Property
read only
bool
Gets a value indicating if this client is connected to the robot
OperationTimeout
Property
TimeSpan
Gets or sets the operation timeout.
ProtocolVersion
Property
read only
int
Gets sftp protocol version.
WorkingDirectory
Property
read only
string
Gets remote working directory.
AppendAllLines(string, string[])
Method
void
Appends lines to a file, creating the file if it does not already exist.
  • path : The file to append the lines to. The file is created if it does not already exist.
  • contents : The lines to append to the file.
AppendAllLines(string, string[], Encoding)
Method
void
Appends lines to a file by using a specified encoding, creating the file if it does not already exist.
  • path : The file to append the lines to. The file is created if it does not already exist.
  • contents : The lines to append to the file.
  • encoding : The character encoding to use.
AppendAllText(string, string)
Method
void
Appends the specified string to the file, creating the file if it does not already exist.
  • path : The file to append the specified string to.
  • contents : The string to append to the file.
AppendAllText(string, string, Encoding)
Method
void
Appends the specified string to the file, creating the file if it does not already exist.
  • path : The file to append the specified string to.
  • contents : The string to append to the file.
  • encoding : The character encoding to use.
AppendText(string)
Method
StreamWriter
Creates a StreamWriter that appends UTF-8 encoded text to the specified file, creating the file if it does not already exist.
  • path : The path to the file to append to.
AppendText(string, Encoding)
Method
StreamWriter
Creates a StreamWriter that appends text to a file using the specified encoding, creating the file if it does not already exist.
  • path : The path to the file to append to.
  • encoding : The character encoding to use.
BeginDownloadFile(string, Stream)
Method
IAsyncResult
Begins an asynchronous file downloading into the stream.
  • path : The path.
  • output : The output.
BeginDownloadFile(string, Stream, AsyncCallback)
Method
IAsyncResult
Begins an asynchronous file downloading into the stream.
  • path : The path.
  • output : The output.
  • asyncCallback : The method to be called when the asynchronous write operation is completed.
BeginDownloadFile(string, Stream, AsyncCallback, object, Action<ulong>)
Method
IAsyncResult
Begins an asynchronous file downloading into the stream.
  • path : The path.
  • output : The output.
  • asyncCallback : The method to be called when the asynchronous write operation is completed.
  • state : A user-provided object that distinguishes this particular asynchronous write request from other requests.
  • downloadCallback : The download callback.
BeginListDirectory(string, AsyncCallback, object, Action<int>)
Method
IAsyncResult
Begins an asynchronous operation of retrieving list of files in remote directory.
  • path : The path.
  • asyncCallback : The method to be called when the asynchronous write operation is completed.
  • state : A user-provided object that distinguishes this particular asynchronous write request from other requests.
  • listCallback : The list callback.
BeginSynchronizeDirectories(string, string, string, AsyncCallback, object)
Method
IAsyncResult
Begins the synchronize directories.
  • sourcePath : The source path.
  • destinationPath : The destination path.
  • searchPattern : The search pattern.
  • asyncCallback : The async callback.
  • state : The state.
BeginUploadFile(Stream, string)
Method
IAsyncResult
Begins an asynchronous uploading the stream into remote file.
  • input : Data input stream.
  • path : Remote file path.
BeginUploadFile(Stream, string, AsyncCallback)
Method
IAsyncResult
Begins an asynchronous uploading the stream into remote file.
  • input : Data input stream.
  • path : Remote file path.
  • asyncCallback : The method to be called when the asynchronous write operation is completed.
BeginUploadFile(Stream, string, AsyncCallback, object, Action<ulong>)
Method
IAsyncResult
Begins an asynchronous uploading the stream into remote file.
  • input : Data input stream.
  • path : Remote file path.
  • asyncCallback : The method to be called when the asynchronous write operation is completed.
  • state : A user-provided object that distinguishes this particular asynchronous write request from other requests.
  • uploadCallback : The upload callback.
BeginUploadFile(Stream, string, bool, AsyncCallback, object, Action<ulong>)
Method
IAsyncResult
Begins an asynchronous uploading the stream into remote file.
  • input : Data input stream.
  • path : Remote file path.
  • canOverride : Specified whether an existing file can be overwritten.
  • asyncCallback : The method to be called when the asynchronous write operation is completed.
  • state : A user-provided object that distinguishes this particular asynchronous write request from other requests.
  • uploadCallback : The upload callback.
ChangeDirectory(string)
Method
void
Changes remote directory to path.
  • path : New directory path.
ChangePermissions(string, short)
Method
void
Changes permissions of file(s) to specified mode.
  • path : File(s) path, may match multiple files.
  • mode : The mode.
ConnectInternal(string, int, string, string)
Method
void
Connects to the robot's SFTP server.
  • ip : Robot IP address.
  • port : SSH/SFTP TCP port.
  • username : Linux username on the robot controller.
  • password : Password for the specified username.
Create(string)
Method
SftpFileStream
Creates or overwrites a file in the specified path.
  • path : The path and name of the file to create.
Create(string, int)
Method
SftpFileStream
Creates or overwrites the specified file.
  • path : The path and name of the file to create.
  • bufferSize : The maximum number of bytes buffered for reads and writes to the file.
CreateDirectory(string)
Method
void
Creates remote directory specified by path.
  • path : Directory path to create.
CreateText(string)
Method
StreamWriter
Creates or opens a file for writing UTF-8 encoded text.
  • path : The file to be opened for writing.
CreateText(string, Encoding)
Method
StreamWriter
Creates or opens a file for writing text using the specified encoding.
  • path : The file to be opened for writing.
  • encoding : The character encoding to use.
Delete(string)
Method
void
Deletes the specified file or directory.
  • path : The name of the file or directory to be deleted. Wildcard characters are not supported.
DeleteDirectory(string)
Method
void
Deletes remote directory specified by path.
  • path : Directory to be deleted path.
DeleteFile(string)
Method
void
Deletes remote file specified by path.
  • path : File to be deleted path.
Disconnect()
Method
void
Disconnects this client from the SFTP server.
DownloadFile(string, Stream, Action<ulong>)
Method
void
Downloads remote file specified by the path into the stream.
  • path : File to download.
  • output : Stream to write the file into.
  • downloadCallback : The download callback.
DownloadFile(string, string, Action<ulong>)
Method
void
Downloads remote file specified by the path into the stream.
  • path : File to download.
  • localPath : Local file to download
  • downloadCallback : The download callback.
EndDownloadFile(IAsyncResult)
Method
void
Ends an asynchronous file downloading into the stream.
  • asyncResult : The pending asynchronous SFTP request.
EndListDirectory(IAsyncResult)
Method
SftpFile[]
Ends an asynchronous operation of retrieving list of files in remote directory.
  • asyncResult : The pending asynchronous SFTP request.
EndSynchronizeDirectories(IAsyncResult)
Method
FileInfo[]
Ends the synchronize directories.
  • asyncResult : The async result.
EndUploadFile(IAsyncResult)
Method
void
Ends an asynchronous uploading the stream into remote file.
  • asyncResult : The pending asynchronous SFTP request.
EnumerateInstallations()
Method
string[]
Enumerates installations with .installation extension. It searches recursively installations in "/programs" if it exists, or "/home/ur/ursim-current/programs" for simulator
EnumeratePrograms()
Method
string[]
Enumerates programs with .urp extension. It searches recursively programs in "/programs" if it exists, or "/home/ur/ursim-current/programs" for simulator
Exists(string)
Method
bool
Checks whether file or directory exists;
  • path : The path.
Get(string)
Method
SftpFile
Gets reference to remote file or directory.
  • path : The path.
GetAttributes(string)
Method
SftpFileAttributes
Gets the SftpFileAttributes of the file on the path.
  • path : The path to the file.
GetLastAccessTime(string)
Method
DateTime
Returns the date and time the specified file or directory was last accessed.
  • path : The file or directory for which to obtain access date and time information.
GetLastAccessTimeUtc(string)
Method
DateTime
Returns the date and time, in coordinated universal time (UTC), that the specified file or directory was last accessed.
  • path : The file or directory for which to obtain access date and time information.
GetLastWriteTime(string)
Method
DateTime
Returns the date and time the specified file or directory was last written to.
  • path : The file or directory for which to obtain write date and time information.
GetLastWriteTimeUtc(string)
Method
DateTime
Returns the date and time, in coordinated universal time (UTC), that the specified file or directory was last written to.
  • path : The file or directory for which to obtain write date and time information.
GetStatus(string)
Method
SftpFileSytemInformation
Gets status using [email protected] request.
  • path : The path.
ListDirectory(string, Action<int>)
Method
SftpFile[]
Retrieves list of files in remote directory.
  • path : The path.
  • listCallback : The list callback.
Open(string, FileMode)
Method
SftpFileStream
Opens a SftpFileStream on the specified path with read/write access.
  • path : The file to open.
  • mode : A FileMode value that specifies whether a file is created if one does not exist, and determines whether the contents of existing files are retained or overwritten.
Open(string, FileMode, FileAccess)
Method
SftpFileStream
Opens a SftpFileStream on the specified path, with the specified mode and access.
  • path : The file to open.
  • mode : A FileMode value that specifies whether a file is created if one does not exist, and determines whether the contents of existing files are retained or overwritten.
  • access : A FileAccess value that specifies the operations that can be performed on the file.
OpenRead(string)
Method
SftpFileStream
Opens an existing file for reading.
  • path : The file to be opened for reading.
OpenText(string)
Method
StreamReader
Opens an existing UTF-8 encoded text file for reading.
  • path : The file to be opened for reading.
OpenWrite(string)
Method
SftpFileStream
Opens a file for writing.
  • path : The file to be opened for writing.
ReadAllBytes(string)
Method
byte[]
Opens a binary file, reads the contents of the file into a byte array, and closes the file.
  • path : The file to open for reading.
ReadAllLines(string)
Method
string[]
Opens a text file, reads all lines of the file using UTF-8 encoding, and closes the file.
  • path : The file to open for reading.
ReadAllLines(string, Encoding)
Method
string[]
Opens a file, reads all lines of the file with the specified encoding, and closes the file.
  • path : The file to open for reading.
  • encoding : The encoding applied to the contents of the file.
ReadAllText(string)
Method
string
Opens a text file, reads all lines of the file with the UTF-8 encoding, and closes the file.
  • path : The file to open for reading.
ReadAllText(string, Encoding)
Method
string
Opens a file, reads all lines of the file with the specified encoding, and closes the file.
  • path : The file to open for reading.
  • encoding : The encoding applied to the contents of the file.
ReadLines(string)
Method
string[]
Reads the lines of a file with the UTF-8 encoding.
  • path : The file to read.
ReadLines(string, Encoding)
Method
string[]
Read the lines of a file that has a specified encoding.
  • path : The file to read.
  • encoding : The encoding that is applied to the contents of the file.
RenameFile(string, string)
Method
void
Renames remote file from old path to new path.
  • oldPath : Path to the old file location.
  • newPath : Path to the new file location.
RenameFile(string, string, bool)
Method
void
Renames remote file from old path to new path.
  • oldPath : Path to the old file location.
  • newPath : Path to the new file location.
  • isPosix : if set to true then perform a posix rename.
SetAttributes(string, SftpFileAttributes)
Method
void
Sets the specified SftpFileAttributes of the file on the specified path.
  • path : The path to the file.
  • fileAttributes : The desired SftpFileAttributes.
SymbolicLink(string, string)
Method
void
Creates a symbolic link from old path to new path.
  • path : The old path.
  • linkPath : The new path.
SynchronizeDirectories(string, string, string)
Method
FileInfo[]
Synchronizes the directories.
  • sourcePath : The source path.
  • destinationPath : The destination path.
  • searchPattern : The search pattern.
UploadFile(Stream, string, Action<ulong>)
Method
void
Uploads stream into remote file.
  • input : Data input stream.
  • path : Remote file path.
  • uploadCallback : The upload callback.
UploadFile(Stream, string, bool, Action<ulong>)
Method
void
Uploads stream into remote file.
  • input : Data input stream.
  • path : Remote file path.
  • canOverride : if set to true then existing file will be overwritten.
  • uploadCallback : The upload callback.
UploadFile(string, string, Action<ulong>)
Method
void
Uploads file into remote file.
  • localPath : Local file to read and transfer.
  • path : Remote file path.
  • uploadCallback : The upload callback.
WriteAllBytes(string, byte[])
Method
void
Writes the specified byte array to the specified file, and closes the file.
  • path : The file to write to.
  • bytes : The bytes to write to the file.
WriteAllLines(string, string[])
Method
void
Writes a collection of strings to the file using the UTF-8 encoding, and closes the file.
  • path : The file to write to.
  • contents : The lines to write to the file.
WriteAllText(string, string)
Method
void
Writes the specified string to the file using the UTF-8 encoding, and closes the file.
  • path : The file to write to.
  • contents : The string to write to the file.
WriteAllText(string, string, Encoding)
Method
void
Writes the specified string to the file using the specified encoding, and closes the file.
  • path : The file to write to.
  • contents : The string to write to the file.
  • encoding : The encoding to apply to the string.
Class
SftpFile
C#Python

Represents SFTP file information

MemberTypeDescription
Attributes
Property
read only
SftpFileAttributes
Gets the file attributes.
FullName
Property
read only
string
Gets the full path of the directory or file.
GroupCanExecute
Property
bool
Gets or sets a value indicating whether the group members can execute this file.
GroupCanRead
Property
bool
Gets or sets a value indicating whether the group members can read from this file.
GroupCanWrite
Property
bool
Gets or sets a value indicating whether the group members can write into this file.
GroupId
Property
int
Gets or sets file group id.
IsBlockDevice
Property
read only
bool
Gets a value indicating whether file represents a block device.
IsCharacterDevice
Property
read only
bool
Gets a value indicating whether file represents a character device.
IsDirectory
Property
read only
bool
Gets a value indicating whether file represents a directory.
IsNamedPipe
Property
read only
bool
Gets a value indicating whether file represents a named pipe.
IsRegularFile
Property
read only
bool
Gets a value indicating whether file represents a regular file.
IsSocket
Property
read only
bool
Gets a value indicating whether file represents a socket.
IsSymbolicLink
Property
read only
bool
Gets a value indicating whether file represents a symbolic link.
LastAccessTime
Property
DateTime
Gets or sets the time the current file or directory was last accessed.
LastAccessTimeUtc
Property
DateTime
Gets or sets the time, in coordinated universal time (UTC), the current file or directory was last accessed.
LastWriteTime
Property
DateTime
Gets or sets the time when the current file or directory was last written to.
LastWriteTimeUtc
Property
DateTime
Gets or sets the time, in coordinated universal time (UTC), when the current file or directory was last written to.
Length
Property
read only
long
Gets or sets the size, in bytes, of the current file.
Name
Property
read only
string
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.
OthersCanExecute
Property
bool
Gets or sets a value indicating whether the others can execute this file.
OthersCanRead
Property
bool
Gets or sets a value indicating whether the others can read from this file.
OthersCanWrite
Property
bool
Gets or sets a value indicating whether the others can write into this file.
OwnerCanExecute
Property
bool
Gets or sets a value indicating whether the owner can execute this file.
OwnerCanRead
Property
bool
Gets or sets a value indicating whether the owner can read from this file.
OwnerCanWrite
Property
bool
Gets or sets a value indicating whether the owner can write into this file.
UserId
Property
int
Gets or sets file user id.
Delete()
Method
void
Permanently deletes a file on remote machine.
MoveTo(string)
Method
void
Moves a specified file to a new location on remote machine, providing the option to specify a new file name.
  • destFileName : The path to move the file to, which can specify a different file name.
SetPermissions(short)
Method
void
Sets file permissions.
  • mode : The mode.
ToString()
Method
string
Returns a string that represents this instance.
UpdateStatus()
Method
void
Updates file status on the server.
Class
SftpFileAttributes
C#Python

Contains SFTP file attributes.

MemberTypeDescription
Extensions
Property
read only
IDictionary<string,string>
Gets or sets the extensions.
GroupCanExecute
Property
bool
Gets a value indicating whether the group members can execute this file.
GroupCanRead
Property
bool
Gets a value indicating whether the group members can read from this file.
GroupCanWrite
Property
bool
Gets a value indicating whether the group members can write into this file.
GroupId
Property
int
Gets or sets file group id.
IsBlockDevice
Property
read only
bool
Gets a value indicating whether file represents a block device.
IsCharacterDevice
Property
read only
bool
Gets a value indicating whether file represents a character device.
IsDirectory
Property
read only
bool
Gets a value indicating whether file represents a directory.
IsNamedPipe
Property
read only
bool
Gets a value indicating whether file represents a named pipe.
IsRegularFile
Property
read only
bool
Gets a value indicating whether file represents a regular file.
IsSocket
Property
read only
bool
Gets a value indicating whether file represents a socket.
IsSymbolicLink
Property
read only
bool
Gets a value indicating whether file represents a symbolic link.
LastAccessTime
Property
DateTime
Gets or sets the local time the current file or directory was last accessed.
LastAccessTimeUtc
Property
DateTime
Gets or sets the UTC time the current file or directory was last accessed.
LastWriteTime
Property
DateTime
Gets or sets the local time when the current file or directory was last written to.
LastWriteTimeUtc
Property
DateTime
Gets or sets the UTC time when the current file or directory was last written to.
OthersCanExecute
Property
bool
Gets a value indicating whether the others can execute this file.
OthersCanRead
Property
bool
Gets a value indicating whether the others can read from this file.
OthersCanWrite
Property
bool
Gets a value indicating whether the others can write into this file.
OwnerCanExecute
Property
bool
Gets a value indicating whether the owner can execute this file.
OwnerCanRead
Property
bool
Gets a value indicating whether the owner can read from this file.
OwnerCanWrite
Property
bool
Gets a value indicating whether the owner can write into this file.
Size
Property
long
Gets or sets the size, in bytes, of the current file.
UserId
Property
int
Gets or sets file user id.
GetBytes()
Method
byte[]
Returns a byte array representing the current SftpFileAttributes.
SetPermissions(short)
Method
void
Sets the permissions.
  • mode : The mode.

What to read next

  • Transfer files and backups: a backup of the programs and the upload of a program.
  • Program and installation files: open and change a .urp file on the PC.

Intégrez facilement les robots Universal Robots, Fanuc, Yaskawa, ABB ou Staubli dans vos applications .NET, Python, LabVIEW ou Matlab

UnderAutomation
Contactez-nousLegal

© All rights reserved.