SFTP file handling

Connect to the robot via SFTP

By default, when calling the Connect() method, the SDK connects in SFTP with the default login "ur" and the password "easybot".

You can change this behavior by specifying the optional arguments enableSFTP, login and password.

Once you are connected, you can check that you are connected with the property SftpEnabled.

_ur = new UR();
_ur.Connect("192.168.0.1", enableSFTP: true, login: "ur", password: "easybot");
bool isEnabled = _ur.SftpEnabled;

Enable and disable SFTP connection

You can disconnect the SFTP connection at any time or reconnect with the methods EnableSFTP and DisableSFTP.

// Connect to the robot without SSH
_ur = new UR();
_ur.Connect("192.168.0.1", enableSFTP: false);
// Enable SFTP later
_ur.EnableSFTP("ur", "easybot");
// Disconnect from SFTP
_ur.DisableSFTP();

SFTP features

SFTP (Secure FTP or Secure File Transfer Protocol) allows you to remotely manipulate files: create, delete, modify, rename, upload, download, enumerate, ...

// Retrieve a file from the robot
using (var content = new FileStream(@"C:\temp\my-program.urp", FileMode.Create)) {
_ur.SFTP.DownloadFile("/home/ur/ursim-current/programs/my-program.urp", content, (bytes) => Console.WriteLine($"Total bytes downloaded : {bytes}"));
}
// send file my-program.urp to the robot
using (var content = new FileStream(@"C:\temp\my-program.urp", FileMode.Create))
{
_ur.SFTP.UploadFile(content, "/home/ur/ursim-current/programs/my-program.urp", (bytes) => Console.WriteLine($"Total bytes uploaded : {bytes}"));
}
// Rename a file
_ur.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 _ur.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
_ur.SFTP.Delete("/home/ur/ursim-current/programs/my-program.urp");
// Write in file
_ur.SFTP.WriteAllText("/home/ur/ursim-current/programs/file.txt", "Hello !");

Here are all the methods you can call on _ur.SFTP :

Members of SSH.SftpClient :
public class SftpClient : BaseClient, IDisposable, ISftpClient {
// Appends lines to a file, creating the file if it does not already exist.
public void AppendAllLines(string path, IEnumerable<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, IEnumerable<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),
// 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),
// Releases unmanaged and - optionally - managed resources
protected override void Dispose(bool disposing),
// Downloads remote file specified by the path into the stream.
public void DownloadFile(string path, Stream output, 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 IEnumerable<SftpFile> EndListDirectory(IAsyncResult asyncResult),
// Ends the synchronize directories.
public IEnumerable<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.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 statvfs@openssh.com request.
public SftpFileSytemInformation GetStatus(string path),
// Retrieves list of files in remote directory.
public IEnumerable<SftpFile> ListDirectory(string path, Action<int> listCallback = null),
// Called when client is connected to the server.
protected override void OnConnected(),
// Called when client is disconnecting from the server.
protected override void OnDisconnecting(),
// Opens a <xref href="UnderAutomation.UniversalRobots.SSH.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.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 IEnumerable<string> ReadLines(string path),
// Read the lines of a file that has a specified encoding.
public IEnumerable<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.Sftp.SftpFileAttributes" data-throw-if-not-resolved="false"></xref> of the file on the specified path.
public void SetAttributes(string path, SftpFileAttributes fileAttributes),
// Sets the date and time the specified file was last accessed.
[Obsolete("Note: This method currently throws NotImplementedException because it has not yet been implemented.")]
public void SetLastAccessTime(string path, DateTime lastAccessTime),
// Sets the date and time, in coordinated universal time (UTC), that the specified file was last accessed.
[Obsolete("Note: This method currently throws NotImplementedException because it has not yet been implemented.")]
public void SetLastAccessTimeUtc(string path, DateTime lastAccessTimeUtc),
// Sets the date and time that the specified file was last written to.
[Obsolete("Note: This method currently throws NotImplementedException because it has not yet been implemented.")]
public void SetLastWriteTime(string path, DateTime lastWriteTime),
// Sets the date and time, in coordinated universal time (UTC), that the specified file was last written to.
[Obsolete("Note: This method currently throws NotImplementedException because it has not yet been implemented.")]
public void SetLastWriteTimeUtc(string path, DateTime lastWriteTimeUtc),
// Creates a symbolic link from old path to new path.
public void SymbolicLink(string path, string linkPath),
// Synchronizes the directories.
public IEnumerable<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),
// 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, IEnumerable<string> contents),
// Writes a collection of strings to the file using the specified encoding, and closes the file.
public void WriteAllLines(string path, IEnumerable<string> contents, Encoding encoding),
// Write the specified string array to the file using the UTF-8 encoding, and closes the file.
public void WriteAllLines(string path, string[] contents),
// Writes the specified string array to the file by using the specified encoding, and closes the file.
public void WriteAllLines(string path, string[] contents, Encoding encoding),
// 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.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.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.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; }
}
Logo

UnderAutomation
66 Avenue des Champs-Elysées
75008 Paris
FRANCE

© 2021 UnderAutomation - UnderAutomation Universal Robots .NET Communication SDK (Software Development Kit) offre la possibilité de créer des applications personnalisées .NET Core, .NET Framework et .NET Standard qui peuvent communiquer avec tous les robots UR. Des exemples prêts à l'emploi pour Windows, Mac OS et Linux, LabView, Node.js et Python sont fournis. Ce SDK utilise le serveur de Dashboard Server TCP/IP et XML-RPC pour contrôler le robot à distance et le protocole Client Interface pour recevoir et décoder le flux de données.