Connect to an SFTP server In order to use the Ftp and other classes, you have to add references to ComponentPro.Sftp.dll, ComponentPro.Common.dll, ComponentPro.Network.dll, and ComponentPro.FileSystem.dll assemblies to your project. To authenticate to an SFTP server, you can simply perform the following steps. Add using directives to your code to create aliases for existing namespaces and avoid having to type the fully qualified type names. Create a new instance of the Sftp class.
Now you can connect to the SFTP server with the Connect methods. Call the Disconnect method to close the session when done. // Create a new class instance. Sftp client = new Sftp; // Connect to the SFTP server. Client.Connect('myserver'); // Or you can specify the SFTP port with // client.Connect('myserver', 22); // Authenticate.
Client.Authenticate('userName', 'password'); // Do something here. Client.DownloadFile('/my remote file.dat', 'my local file'); // Disconnect. Client.Disconnect; ' Create a new class instance. Dim client As New Sftp ' Connect to the SFTP server. Client.Connect('myserver') ' Or you can specify the SFTP port with ' client.Connect('myserver', 22); ' Authenticate. Client.Authenticate('userName', 'password') ' Do something here. Client.DownloadFile('/my remote file.dat', 'my local file') ' Disconnect.
Proxy servers The Ultimate SFTP component fully supports many proxy servers (often referred to as 'proxies'). If you need to connect to your SFTP server through a proxy, simply create a new instance of the FtpProxy class, set the appropriate properties of the WebProxyEx object, and assign it to the Proxy property of the Sftp class, and the necessary proxy communication will take place. Ultimate SFTP component supports the following Proxy servers: Proxy Name SOCKS4 SOCKS4 proxy. SOCKS4A SOCKS4A proxy (capable of resolving domain names). SOCKS5 SOCKS5 proxy.
![]()
Upload a stream of data to the FTP server in the current working directory. FTP server in the current working directory. Namespace: EnterpriseDT.Net.Ftp. Visual Basic. If the stream is left open the its position will be at the end of the stream. InStr = new MemoryStream(bytes); // upload the stream to a file on the server.
HTTP CONNECT HTTP proxy using CONNECT method. // Create a new class instance. Sftp client = new Sftp; // Connect to the SFTP server. Client.Connect('myserver', 22); // Authenticate user with a private key. Client.Authenticate('userName', 'c: privateKey.key', 'pkeypassword'); // It's possible to use both username/password and private key to authenticate. // client.Authenticate('userName', 'password', 'c: privateKey.key', 'pkeypassword'); // Do something here.
Client.DownloadFile('/my remote file.dat', 'my local file'); // Disconnect. Client.Disconnect; ' Create a new class instance. Dim client As New Sftp ' Connect to the SFTP server. Client.Connect('myserver', 22) ' Authenticate user with a private key.
Client.Authenticate('userName', 'c: privateKey.key', 'pkeypassword') ' It's possible to use both username/password and private key to authenticate. ' client.Authenticate('userName', 'password', 'c: privateKey.key', 'pkeypassword'); ' Do something here. Client.DownloadFile('/my remote file.dat', 'my local file') ' Disconnect. Authenticate with a key It is more desirable to use public key cryptography rather than using a password to authenticate. The SSH server keeps a database of public keys for each account - exact details of this depends on the server (OpenSSH keeps the public keys in /.ssh/authorizedkeys file in each account's home directory). A public key has a corresponding private key that is kept secret by the client.
To authenticate to the server using public key cryptography, you need this private key. To authenticate using your private key, simply call the Authenticate method with the SecureShellPrivateKey object created from the provided name of the private key and its passphrase. If you find out that your private key format is not supported by our component, please do not hesitate to send a request to us at [email protected]. // Create a new class instance. Sftp client = new Sftp; // Connect to the SFTP server. Client.Connect('localhost'); // Authenticate. Client.Authenticate('test', 'test'); //.
FileStream fi = new FileStream('c: test.dat', FileMode.Open); // Upload data from a stream to '/test.dat'. Client.UploadFile(fi, '/test.dat'); fi.Close; //. // Disconnect. Client.Disconnect; ' Create a new class instance.
Dim client As New Sftp ' Connect to the SFTP server. Client.Connect('localhost') ' Authenticate. Client.Authenticate('test', 'test') '. Dim fi As New FileStream('c: test.dat', FileMode.Open) ' Upload data from a stream to '/test.dat'.
Client.UploadFile(fi, '/test.dat') fi.Close '. ' Disconnect. // Create a new class instance. Sftp client = new Sftp; // Connect to the SFTP server. Client.Connect('localhost'); // Authenticate. Client.Authenticate('test', 'test'); //.
// Create new file FileStream fo = new FileStream('c: file.dat', FileMode.Create); // Download remote file '/test.dat' to 'c: test.dat' client.DownloadFile('/test.dat', fo); fo.Close; //. // Disconnect. Client.Disconnect; ' Create a new class instance.
Dim client As New Sftp ' Connect to the SFTP server. Client.Connect('localhost') ' Authenticate. Client.Authenticate('test', 'test') '. ' Create new file Dim fo As New FileStream('c: file.dat', FileMode.Create) ' Download remote file '/test.dat' to 'c: test.dat' client.DownloadFile('/test.dat', fo) fo.Close '. ' Disconnect. // Create a new instance of the Sftp class.
Sftp client = new Sftp; // Turn on transfer compression. By default, the EnableCompression property is set to false. Client.Config.EnableCompression = true; // If you wish to turn off transfer compression // client.EnableCompression = false; // Connect to the server client.Connect('server', 22); //.
Client.Disconnect; ' Create a new instance of the Sftp class. Dim client As New Sftp ' Turn on transfer compression. By default, the EnableCompression property is set to false. Client.Config.EnableCompression = True ' If you wish to turn off transfer compression ' client.EnableCompression = false; ' Connect to the server client.Connect('server', 22) '. // Create a new Sftp instance. Sftp client = new Sftp; // Connect to the SFTP server.
Client.Connect('localhost'); // Authenticate. Client.Authenticate('test', 'test'); //. // Resume uploading local file 'c: test.dat' to '/test.dat'. Client.ResumeUploadFile('c: test.dat', '/test.dat'); //. // Disconnect. Client.Disconnect; ' Create a new Sftp instance.
Dim client As New Sftp ' Connect to the SFTP server. Client.Connect('localhost') ' Authenticate. Client.Authenticate('test', 'test') '. ' Resume uploading local file 'c: test.dat' to '/test.dat'.
Client.ResumeUploadFile('c: test.dat', '/test.dat') '. ' Disconnect. ASCII and binary transfer modes Ultimate SFTP supports two transfer mode: Binary (default) and ASCII. In binary mode (by setting the TransferType property to Binary), data is transferred as is without any processing. When text data is transferred between platforms that use different end-of-line sequences (Windows use Unix uses ), the transferred text data is unchanged and may be unfriendly presented to the user.
It's essential to have the library convert the sequences automatically. To achieve that, set the TransferType property to Ascii. // Create a new class instance. Sftp client = new Sftp; // Connect to the SFTP server. Client.Connect('localhost'); // Authenticate.
Client.Authenticate('test', 'test'); //. // Download remote file '/test.dat' to 'c: test.dat' client.DownloadFile('/test.dat', 'c: test.dat', 1024, 1024, -1); //. // Disconnect. Client.Disconnect; ' Create a new class instance.
Dim client As New Sftp ' Connect to the SFTP server. Client.Connect('localhost') ' Authenticate. Client.Authenticate('test', 'test') '. ' Download remote file '/test.dat' to 'c: test.dat' client.DownloadFile('/test.dat', 'c: test.dat', 1024, 1024, -1) '. ' Disconnect. Large file support Ultimate SFTP supports two transfer modes: Binary and ASCII. The SFTP protocol's maximum supported file size is 2^63 bytes = 9,223,372 TB.
However, some underlying filesystems do not support large file size. FAT16 and FAT32 are two examples; they support 2GB and 4GB maximum file size respectively. For other filesystems, see this.
There is still a workaround for the filesystems that can't store files larger than 4GB. The stream-based file transfer API supports reading file from small pieces and combine them all together to have the large file. // Create a new class instance. Sftp client = new Sftp; // Connect to the SFTP server.
Client.Connect('localhost'); // Authenticate. Client.Authenticate('test', 'test'); //. // Upload local file 'c: test.dat' to '/test.dat'.
Client.UploadFile('c: test.dat', '/test.dat'); //. // Disconnect. Client.Disconnect; ' Create a new class instance. Dim client As New Sftp ' Connect to the SFTP server. Client.Connect('localhost') ' Authenticate. Client.Authenticate('test', 'test') '.
' Upload local file 'c: test.dat' to '/test.dat'. Client.UploadFile('c: test.dat', '/test.dat') '. ' Disconnect. Append data to an existing file The UploadFile and DownloadFile methods make it possible to upload or download a part of a file. This is often used for appending data to an existing file, or to resume broken transfers. Of course, it is possible to write/read data to/from any other position in a file as well.
To upload and download specific file part, use three additional parameters: local offset, remote offset, and the number of bytes you want to transfer. Local offset is not required for stream-based methods - data is read/written from/to the current stream position. // Connect to an FTP file system. Ftp ftpsys = new Ftp; ftpsys.Connect('192.168.126.128', 21); ftpsys.Authenticate('test', 'test'); // Connect to an SFTP file system. Sftp sftp = new Sftp; sftp.Connect('192.168.126.128', 2222); sftp.Authenticate('test', 'test'); // Copy 'my blog on SFTP file system.txt' file from the SFTP file system to the FTP file system.
Sftp.CopyTo('my blog on SFTP file system.txt', ftpsys, 'blog.txt'); ftpsys.Disconnect; sftp.Disconnect; ' Connect to an FTP file system. Dim ftpsys As New Ftp ftpsys.Connect('192.168.126.128', 21) ftpsys.Authenticate('test', 'test') ' Connect to an SFTP file system.
Dim sftp As New Sftp sftp.Connect('192.168.126.128', 2222) sftp.Authenticate('test', 'test') ' Copy 'my blog on SFTP file system.txt' file from the SFTP file system to the FTP file system. Sftp.CopyTo('my blog on SFTP file system.txt', ftpsys, 'blog.txt') ftpsys.Disconnect sftp.Disconnect. // Connect to an FTP file system.
Ftp ftpsys = new Ftp; ftpsys.Connect('192.168.126.128', 21); ftpsys.Authenticate('test', 'test'); // Connect to an SFTP file system. Sftp sftp = new Sftp; sftp.Connect('192.168.126.128', 2222); sftp.Authenticate('test', 'test'); // Copy 'blog.txt' file from the SFTP file system to the FTP file system. Sftp.CopyTo('blog.txt', ftpsys, 'my blog on FTP file system.txt'); ftpsys.Disconnect; sftp.Disconnect; ' Connect to an FTP file system. Dim ftpsys As New Ftp ftpsys.Connect('192.168.126.128', 21) ftpsys.Authenticate('test', 'test') ' Connect to an SFTP file system. Dim sftp As New Sftp sftp.Connect('192.168.126.128', 2222) sftp.Authenticate('test', 'test') ' Copy 'blog.txt' file from the SFTP file system to the FTP file system. Sftp.CopyTo('blog.txt', ftpsys, 'my blog on FTP file system.txt') ftpsys.Disconnect sftp.Disconnect.
// Create a new class instance. Sftp client = new Sftp; // Connect to the SFTP server. Client.Connect('myserver'); // Authenticate. Client.Authenticate('userName', 'password'); //. // Delete remote file '/test.dat'. Client.DeleteFile('/test.dat'); //.
// Disconnect. Client.Disconnect; ' Create a new class instance. Dim client As New Sftp ' Connect to the SFTP server.
Client.Connect('myserver') ' Authenticate. Client.Authenticate('userName', 'password') '. ' Delete remote file '/test.dat'. Client.DeleteFile('/test.dat') '.
' Disconnect. // Create a new class instance. Sftp client = new Sftp; // Connect to the SFTP server.
Client.Connect('myserver'); // Authenticate. Client.Authenticate('userName', 'password'); //. // Determine whether file 'myFile.dat' exists or not.
If (client.FileExists('/myFile.dat')) Console.WriteLine('File '/myFile.dat' exists'); else Console.WriteLine('File '/myFile.dat' does not exist'); //. // Disconnect. Client.Disconnect; ' Create a new class instance. Dim client As New Sftp ' Connect to the SFTP server.
Client.Connect('myserver') ' Authenticate. Client.Authenticate('userName', 'password') '. ' Determine whether file 'myFile.dat' exists or not. If client.FileExists('/myFile.dat') Then Console.WriteLine('File '/myFile.dat' exists') Else Console.WriteLine('File '/myFile.dat' does not exist') End If '. ' Disconnect. // Create a new class instance.
Sftp client = new Sftp; // Connect to the SFTP server. Client.Connect('myserver'); // Authenticate.
Client.Authenticate('userName', 'password'); //. // Rename remote file '/file.dat' to 'myfile.dat' client.Rename('/file.dat', '/myfile.dat'); // Move a file client.Rename('/file2.dat', '/new-folder/file2.dat'); // Rename a remote directory client.Rename('/album-folder', '/old-album-folder'); // Move a directory client.Rename('/album-folder2', '/new-folder/album-folder2'); //. // Disconnect.
Client.Disconnect; ' Create a new class instance. Dim client As New Sftp ' Connect to the SFTP server. Client.Connect('myserver') ' Authenticate. Client.Authenticate('userName', 'password') '. ' Rename remote file '/file.dat' to 'myfile.dat' client.Rename('/file.dat', '/myfile.dat') ' Move a file client.Rename('/file2.dat', '/new-folder/file2.dat') ' Rename a remote directory client.Rename('/album-folder', '/old-album-folder') ' Move a directory client.Rename('/album-folder2', '/new-folder/album-folder2') '.
' Disconnect. // Create a new class instance. Sftp client = new Sftp; // Connect to the SFTP server. Client.Connect('myserver'); // Authenticate. Client.Authenticate('userName', 'password'); //. // Set file attributes SftpFileAttributes attrs = new SftpFileAttributes; attrs.Permissions = SftpFilePermissions.OwnerExecute SftpFilePermissions.OwnerWrite SftpFilePermissions.OwnerRead; attrs.LastWriteTime = DateTime.Now; client.SetFileAttributes('/file.dat', attrs); //. // Disconnect.
Client.Disconnect; ' Create a new class instance. Dim client As New Sftp ' Connect to the SFTP server.
Client.Connect('myserver') ' Authenticate. Client.Authenticate('userName', 'password') '. ' Set file attributes Dim attrs As New SftpFileAttributes attrs.Permissions = SftpFilePermissions.OwnerExecute Or SftpFilePermissions.OwnerWrite Or SftpFilePermissions.OwnerRead attrs.LastWriteTime = Date.Now client.SetFileAttributes('/file.dat', attrs) '. ' Disconnect. File masks File masks consist of three types of symbols and their combinations:. Fixed characters: Any letter, number or character that is allowed in a file name. For example, pic.jpg refers to a single file named as ‘pic.jpg’.
Question Mark (?): A question mark (?) denotes any single character. For example,?in.docx can refer to every file with a name consisting of three letters ending with ‘in’ and.docx as an extension like tin.docx, kin.docx, fin.docx, etc. Asterisk ( ): An asterisk refers to various characters of an indefinite number. For example, at.docx can refer to all.docx files with a name starting with ‘a’ and ending with ‘t’ and can contain any number of characters in between like at.docx, ant.docx, aunt.docx, etc. '.' is the special wildcard that is used to match any character, including directory separators. See this for more details.
The above examples show that a file mask with only fixed characters refers to a single unique file (e.g. File mask my-picture.jpg refers to, and only to, a file named my-picture.jpg), while file masks containing wildcards such as ‘?’ or/and ‘.’ can refer to both single or multiple files. // Create a new class instance. Sftp client = new Sftp; // Connect to the SFTP server. Client.Connect('localhost'); // Authenticate. Client.Authenticate('test', 'test'); //.
// Upload all files and subdirectories from local folder 'c: temp' to the remote dir '/temp' client.Upload('c: temp', '/temp'); // Upload all directories, subdirectories, and files that match the specified search pattern from local folder 'c: myfolder2' to remote folder '/myfolder2'. Client.Upload('c: myfolder2', '/myfolder2', '.cs'); // or you can simply put wildcard masks in the source path, our component will automatically parse it.
// upload all.css files from local folder 'c: myfolder2' to remote folder '/myfolder2'. Client.Upload('c: myfolder2.css', '/myfolder2'); // Upload.cs and.vb files from local folder 'c: myfolder2' to remote folder '/myfolder2'. Client.Upload('c: myfolder2.cs;.vb', '/myfolder2'); //. // Disconnect.
Client.Disconnect; ' Create a new class instance. Dim client As New Sftp ' Connect to the SFTP server. Client.Connect('localhost') ' Authenticate. Client.Authenticate('test', 'test') '. ' Upload all files and subdirectories from local folder 'c: temp' to the remote dir '/temp' client.Upload('c: temp', '/temp') ' Upload all directories, subdirectories, and files that match the specified search pattern from local folder 'c: myfolder2' to remote folder '/myfolder2'. Client.Upload('c: myfolder2', '/myfolder2', '.cs') ' or you can simply put wildcard masks in the source path, our component will automatically parse it.
' upload all.css files from local folder 'c: myfolder2' to remote folder '/myfolder2'. Client.Upload('c: myfolder2.css', '/myfolder2') ' Upload.cs and.vb files from local folder 'c: myfolder2' to remote folder '/myfolder2'. Client.Upload('c: myfolder2.cs;.vb', '/myfolder2') '.
' Disconnect. // Create a new class instance. Sftp client = new Sftp; // Connect to the SFTP server. Client.Connect('myserver'); // Authenticate. Client.Authenticate('userName', 'password'); //. // Get all directories, subdirectories, and files from remote folder '/myfolder' to 'c: myfolder'. Client.Download('/myfolder', 'c: myfolder'); // Get all directories, subdirectories, and files that match the specified search pattern from remote folder '/myfolder2' to 'c: myfolder2'.
Client.Download('/myfolder2', 'c: myfolder2', '.cs'); // or you can simply put wildcard masks in the source path, our component will automatically parse it. // download all.css files from remote folder '/myfolder2' to local folder 'c: myfolder2'. Client.Download('/myfolder2/.css', 'c: myfolder2'); // Download.cs and.vb files from remote folder '/myfolder2' to local folder 'c: myfolder2'.
Client.Download('/myfolder2/.cs;.vb', 'c: myfolder2'); // Get files in the folder '/myfolder2' only. TransferOptions opt = new TransferOptions(true, false, OptionValue.Auto, null, FileOverwriteMode.Overwrite, SymlinksResolveAction.Skip); client.Download('/myfolder2', 'c: myfolder2', opt); //. // Disconnect. Client.Disconnect; ' Create a new class instance.
Dim client As New Sftp ' Connect to the SFTP server. Client.Connect('myserver') ' Authenticate. Client.Authenticate('userName', 'password') '. ' Get all directories, subdirectories, and files from remote folder '/myfolder' to 'c: myfolder'.
Client.Download('/myfolder', 'c: myfolder') ' Get all directories, subdirectories, and files that match the specified search pattern from remote folder '/myfolder2' to 'c: myfolder2'. Client.Download('/myfolder2', 'c: myfolder2', '.cs') ' or you can simply put wildcard masks in the source path, our component will automatically parse it. ' download all.css files from remote folder '/myfolder2' to local folder 'c: myfolder2'. Client.Download('/myfolder2/.css', 'c: myfolder2') ' Download.cs and.vb files from remote folder '/myfolder2' to local folder 'c: myfolder2'. Client.Download('/myfolder2/.cs;.vb', 'c: myfolder2') ' Get files in the folder '/myfolder2' only. Dim opt As New TransferOptions(True, False, OptionValue.Auto, Nothing, FileOverwriteMode.Overwrite, SymlinksResolveAction.Skip) client.Download('/myfolder2', 'c: myfolder2', opt) '. ' Disconnect.
Transfer statistics The result of a multiple file operation can be accessed through a FileSystemTransferStatistics object. It provides useful information shown below:.
FilesProcessed/ DirectoriesProcessed - The number of files/directories processed. FilesSkipped/ DirectoriesSkipped - The number of files/directories skipped. TotalFiles/ TotalDirectories - The total number of files/directories.
TotalBytes, TotalBytesSkipped, TotalBytesTransferred - The total number of bytes, skipped bytes, and processed bytes. BytesPerSecond - Bytes per second.
FileList - List of files and directories (available only when TransferOptions.RetainFileList is true). Automatic ASCII file type classification By default, the data is transferred in binary mode without any processing. However, when transferring text data between platforms which use different end-of-line sequences (Windows use, Unix uses ), it's essential to have the sequences automatically converted by setting the transfer mode to ASCII. In a multi-file transfer, to determine whether a file should be transferred in ASCII or Binary mode, Ultimate SFTP uses the AsciiFileSearchConditions property to check against the processing file. If a file matches that criteria, the file will be transferred in ASCII mode; otherwise, binary mode is used. Resume interrupted transfers When a transfer is interrupted due to a disconnected connection or user abortion, files that are still in transfer progress may be incomplete.
Instead of transferring them all again, you can resume transferring the missing parts of the incomplete files only. By specifying FileOverwriteMode.ResumeFileTransfer as the action on existing files, the Upload and Download methods can do this automatically. The library will do the following:. Transfer files that are not present in the destination directory. Resume transfer incomplete destination files which have a smaller size than on the source ones. Ignore/skip all files that similar name and size. Sftp client = new Sftp; // Connect to the SFTP server.
Client.Connect('myserver', 21); // Authenticate. Client.Authenticate('userName', 'password'); // Create a new instance of the QuickSyncOptions class.
QuickSyncOptions opt = new QuickSyncOptions; // Set synchronization's settings opt.Comparer = FileComparers.FileContentComparer; opt.Recursive = true; // Synchronize folders. Client.QuickSynchronize( '/', // Source directory. 'c: test', // Destination directory. This local dir will be identical to the remote dir ('/'). True, // Remote dir is master opt ); // Do something here //. Client.Disconnect; Dim client As New Sftp ' Connect to the SFTP server.
Client.Connect('myserver', 21) ' Authenticate. Client.Authenticate('userName', 'password') ' Create a new instance of the QuickSyncOptions class. Dim opt As New QuickSyncOptions ' Set synchronization's settings opt.Comparer = FileComparers.FileContentComparer opt.Recursive = True ' Synchronize folders. Client.QuickSynchronize('/', 'c: test', True, opt) ' Remote dir is master - Destination directory. This local dir will be identical to the remote dir ('/'). Source directory.
' Do something here '. Show the progress while transferring data When the file to transfer is big, or the transfer speed is slow, you can let the user know that your app is transferring files by updating a progress bar. The Ultimate SFTP component provides progress notification through the Progress event. The event is raised periodically while data transfer is in progress, making accessible necessary data to display progress information, such as the size of the file and the number of bytes transferred. The following example shows you how to handle the Progress event to show progress information while transferring a file. // Create a new class instance.
Sftp client = new Sftp; // Connect to the SFTP server. Client.Connect('myserver'); // Authenticate. Client.Authenticate('userName', 'password'); //. // Create a new attribute object and set permissions and modifed date time. SftpFileAttributes attrs = new SftpFileAttributes; attrs.Permissions = SftpFilePermissions.OwnerExecute SftpFilePermissions.OwnerWrite SftpFilePermissions.OwnerRead; attrs.LastWriteTime = DateTime.Now; // Set all files.
Client.SetMultipleFilesAttributes('/my folder', attrs, true, null); //. // Disconnect. Client.Disconnect; ' Create a new class instance. Dim client As New Sftp ' Connect to the SFTP server. Client.Connect('myserver') ' Authenticate.
Client.Authenticate('userName', 'password') '. ' Create a new attribute object and set permissions and modifed date time. Dim attrs As New SftpFileAttributes attrs.Permissions = SftpFilePermissions.OwnerExecute Or SftpFilePermissions.OwnerWrite Or SftpFilePermissions.OwnerRead attrs.LastWriteTime = Date.Now ' Set all files. Client.SetMultipleFilesAttributes('/my folder', attrs, True, Nothing) '. ' Disconnect.
Create an empty directory To create a new directory, call the CreateDirectory method of the Sftp class. By default, a new directory will be created in the current working directory. If you want to create the directory in another location, you could use the SetCurrentDirectory to change the current working directory before calling the CreateDirectory method. The method also accepts an absolute path. The following code example shows you how to use the CreateDirectory method to create a new directory on the remote SFTP server. // Create a new class instance. Sftp client = new Sftp; // Connect to the SFTP server.
Client.Connect('myserver'); // Authenticate. Client.Authenticate('userName', 'password'); //. // Create 'myFolder' directory. Client.CreateDirectory('/myFolder'); //.
// Disconnect. Client.Disconnect; ' Create a new class instance. Dim client As New Sftp ' Connect to the SFTP server. Client.Connect('myserver') ' Authenticate. Client.Authenticate('userName', 'password') '. ' Create 'myFolder' directory. Client.CreateDirectory('/myFolder') '.
' Disconnect. // Create a new class instance. Sftp client = new Sftp; // Connect to the SSFTP server. Client.Connect('server'); // Authenticate.
Client.Authenticate('test', 'test'); client.EnsuresDirectoryCreated('/home/data/personal/photos/family-photos'); //. // Disconnect. Client.Disconnect; ' Create a new class instance. Dim client As New Sftp ' Connect to the SSFTP server. Client.Connect('server') ' Authenticate. Client.Authenticate('test', 'test') client.EnsuresDirectoryCreated('/home/data/personal/photos/family-photos') '. ' Disconnect.
// Create a new class instance. Sftp client = new Sftp; // Connect to the SFTP server. Client.Connect('myserver'); // Authenticate.
Client.Authenticate('userName', 'password'); //. // Rename remote file '/file.dat' to 'myfile.dat' client.Rename('/file.dat', '/myfile.dat'); // Move a file client.Rename('/file2.dat', '/new-folder/file2.dat'); // Rename a remote directory client.Rename('/album-folder', '/old-album-folder'); // Move a directory client.Rename('/album-folder2', '/new-folder/album-folder2'); //. // Disconnect. Client.Disconnect; ' Create a new class instance.
Dim client As New Sftp ' Connect to the SFTP server. Client.Connect('myserver') ' Authenticate. Client.Authenticate('userName', 'password') '. ' Rename remote file '/file.dat' to 'myfile.dat' client.Rename('/file.dat', '/myfile.dat') ' Move a file client.Rename('/file2.dat', '/new-folder/file2.dat') ' Rename a remote directory client.Rename('/album-folder', '/old-album-folder') ' Move a directory client.Rename('/album-folder2', '/new-folder/album-folder2') '. ' Disconnect. // Create a new class instance. Sftp client = new Sftp; // Connect to the SFTP server.
Client.Connect('myserver'); // Authenticate. Client.Authenticate('userName', 'password'); //. // Determine whether folder 'myFolder' exists or not. If (client.DirectoryExists('/myFolder')) Console.WriteLine('Folder '/myFolder' exists'); else Console.WriteLine('Folder '/myFolder' does not exist'); //.
// Disconnect. Client.Disconnect; ' Create a new class instance. Dim client As New Sftp ' Connect to the SFTP server. Client.Connect('myserver') ' Authenticate. Client.Authenticate('userName', 'password') '. ' Determine whether folder 'myFolder' exists or not.
If client.DirectoryExists('/myFolder') Then Console.WriteLine('Folder '/myFolder' exists') Else Console.WriteLine('Folder '/myFolder' does not exist') End If '. ' Disconnect. // Create a new class instance.
Sftp client = new Sftp; // Connect to the SFTP server. Client.Connect('localhost'); // Authenticate. Client.Authenticate('test', 'test'); //. // Remove an empty directory. Client.DeleteDirectory('/temp'); // Delete an entire directory.
Client.DeleteDirectory('/test', true); // Delete files with size smaller than 200kb in '/test dir' directory. All empty directories will be removed. Client.Delete('/my folder', true, new SizeSearchCondition(0, 200. 1024)); // Delete.exe files in '/test dir' directory.
All empty directories will be removed. Client.Delete('/test dir', true, '.exe'); // Delete.tmp and.temp files in '/test dir' directory. All empty directories will be removed. // wildcard masks are delimited by ';', ',', and ' ' characters. Client.Delete('/test dir/.tmp;.temp', true); // Delete.cs files in '/test folder' directory.
All empty directories are not to be removed. Client.Delete('/test folder', true, false, new NameSearchCondition('.cs')); //. // Disconnect.
Client.Disconnect; ' Create a new class instance. Dim client As New Sftp ' Connect to the SFTP server. Client.Connect('localhost') ' Authenticate. Client.Authenticate('test', 'test') '. ' Remove an empty directory. Client.DeleteDirectory('/temp') ' Delete an entire directory.
Client.DeleteDirectory('/test', True) ' Delete files with size smaller than 200kb in '/test dir' directory. All empty directories will be removed. Client.Delete('/my folder', True, New SizeSearchCondition(0, 200. 1024)) ' Delete.exe files in '/test dir' directory. All empty directories will be removed. Client.Delete('/test dir', True, '.exe') ' Delete.tmp and.temp files in '/test dir' directory.
All empty directories will be removed. ' wildcard masks are delimited by ';', ',', and ' ' characters. Client.Delete('/test dir/.tmp;.temp', True) ' Delete.cs files in '/test folder' directory. All empty directories are not to be removed.
Client.Delete('/test folder', True, False, New NameSearchCondition('.cs')) '. ' Disconnect. Control bandwidth - set speed limits ComponentPro transport classes (Ftp and Sftp) support so-called bandwidth control - restrictions on the amount of data transferred per second. Bandwidth throttlings are set separately for uploading and downloading data. There are some reasons why an application needs the bandwidth throttling feature.
When the network connection is shared between multiple consumers, high transfer speed is not always desirable. In this situation, it is essential for the application using the component not to consume too much network bandwidth and let other consumers make use of the network as well. In slow and unstable networks, lower speed of transfer usually increases stability and decreases the chance of network failure and disconnection. MaxUploadSpeed and MaxDownloadSpeed properties of the Sftp component can be used to adjust transfer speed on the fly.
TransferConfirm event When transferring lots of files using the Upload or Download methods, things can occasionally go wrong due to unpredictable problems. Those issues may be:. A target file already exists. Recursive link detected. Directory listing failure. File transfer failure. To be informed about such errors, handle the TransferConfirm event.
In the handler of that event, you can choose how to react to the issue by setting the TransferConfirmEventArgs.NextAction property. The TransferConfirmEventArgs data object of the TransferConfirm event has lots of useful members to help you know what the issue is, what the source and destination files are and what the reaction should be. The following code snippet demonstrates how to handle the TransferConfirm event to append '(1)' phrase to the file name of an existing target file. Unified API for SFTP and FTP FTP and SFTP are two different file transfer protocols, but they both cover same task - transferring files over the network.
Our Unified File System library lets you write the same code for file management with FTP, SFTP, and ZIP. One of the benefits of having similar API across Sftp, Ftp, and Zip classes is that the time required to familiarize yourself with a new protocol is minimized or eliminated altogether. The interface IRemoteFileSystem that both Sftp and Sftp classes implement, defines most Sftp and Ftp methods, properties, and events, making it easy to write the single code segment to work with both protocols as shown in the example below.
// The IRemoteFileSystem interface is used for all remote file systems, including Ftp, Sftp, and Scp. IRemoteFileSystem client; // Create a new class instance.
If (isFtp) client = new Ftp; else client = new Sftp; // Connect to the server client.Connect(serverName, serverPort); // Authenticate the user. Client.Authenticate(userName, password); // Upload files in 'C: data' to '/data'. Client.Upload(@'C: data', '/data'); // Download a remote file. Client.DownloadFile('/content/file.txt', @'c: data file.txt'); // Close the connection.
Client.Disconnect; ' The IRemoteFileSystem interface is used for all remote file systems, including Ftp, Sftp, and Scp. Dim client As IRemoteFileSystem ' Create a new class instance. If isFtp Then client = New Ftp Else client = New Sftp End If ' Connect to the server client.Connect(serverName, serverPort) ' Authenticate the user. Client.Authenticate(userName, password) ' Upload files in 'C: data' to '/data'. Client.Upload('C: data', '/data') ' Download a remote file. Client.DownloadFile('/content/file.txt', 'c: data file.txt') ' Close the connection. Transfer queue Are you looking for a component that has the capability to add files and directories to a queue and start, stop or pause the queue any time?
The TransferQueue class is designed to do that job. The free LionFTP client is an example of how a WinForms application makes use of that class. That class also allows you to add files to transfer, sort file list, set item priority on-the-fly.
It also lets you change the number of threads used to transfer files while the queue is still in process, start, pause and resume the queue. // Create a new class instance. Sftp client = new Sftp; // Connect to the SFTP server.
Client.Connect('myserver'); // Authenticate. Client.Authenticate('userName', 'password'); //. // Get the time difference between the server and the client. TimeSpan ts = client.GetServerTimeDifference; Console.WriteLine('Time difference: ' + ts.ToString); //. // Disconnect. Client.Disconnect; ' Create a new class instance. Dim client As New Sftp ' Connect to the SFTP server.
Client.Connect('myserver') ' Authenticate. Client.Authenticate('userName', 'password') '.
' Get the time difference between the server and the client. Dim ts As TimeSpan = client.GetServerTimeDifference Console.WriteLine('Time difference: ' & ts.ToString) '. ' Disconnect. // Create a new class instance.
Sftp client = new Sftp; // Connect to the SFTP server. Client.Connect('localhost'); // Authenticate. Client.Authenticate('test', 'test'); //. // Download remote file '/test.dat' to 'c: test.dat' long transferred = await client.DownloadFileAsync('/test.dat', 'c: test.dat'); //.
Console.WriteLine('Bytes transferred: ' + transferred); // Disconnect. Client.Disconnect; ' Create a new class instance. Dim client As New Sftp ' Connect to the SFTP server. Client.Connect('localhost') ' Authenticate.
Client.Authenticate('test', 'test') '. ' Download remote file '/test.dat' to 'c: test.dat' Dim transferred As Long = Await client.DownloadFileAsync('/test.dat', 'c: test.dat') '. Console.WriteLine('Bytes transferred: ' & transferred) ' Disconnect.
Use asynchronous methods Sftp provides a number of methods allowing you to asynchronously connect, login, upload, download, etc. Their names always end with Async. This means the method will begin some operation on a new thread and immediately return to the caller.
When the operation has completed, the corresponding event will be raised notifying you that the operation has completed and in the event handler method you can check for the error and use the result, if provided. The example following demonstrates how to connect to an SFTP server and download a file asynchronously.
// Establish a shared SSH connection SecureShellConnection session = new SecureShellConnection; session.Connect(serverName); session.Authenticate(userName, password); // Use the established SSH connection as proxy to connect to the FTP server Ftp ftp = new Ftp; ftp.SetSocketFactory(session.ToSocketFactory); ftp.Connect(ftpServerName); ftp.Authenticate(ftpUserName, ftpPassword); // Use the ftp object to upload, download, delete files, etc. ' Establish a shared SSH connection Dim session As New SecureShellConnection session.Connect(serverName) session.Authenticate(userName, password) ' Use the established SSH connection as proxy to connect to the FTP server Dim ftp As New Ftp ftp.SetSocketFactory(session.ToSocketFactory) ftp.Connect(ftpServerName) ftp.Authenticate(ftpUserName, ftpPassword) ' Use the ftp object to upload, download, delete files, etc. // Create a new class instance. Sftp client = new Sftp; // Connect to the SFTP server. Client.Connect('myserver'); // Authenticate. Client.Authenticate('userName', 'password'); //. // Keep the connection alive.
Client.KeepAlive; //. // Disconnect. Client.Disconnect; ' Create a new class instance. Dim client As New Sftp ' Connect to the SFTP server. Client.Connect('myserver') ' Authenticate. Client.Authenticate('userName', 'password') '. ' Keep the connection alive.
Client.KeepAlive '. ' Disconnect. // Create a new instance of the SecureShellConnection class. SecureShellConnection ssh = new SecureShellConnection; // Connect to the SSH server. Ssh.Connect(serverName); // Authenticate ssh.Authenticate(userName, password); // Create a new instance of the Sftp class // and bind it to the ssh connection. Sftp client = new Sftp; client.ReuseConnection(ssh); // Upload a file. Client.UploadFile(@'D: data test.dat', 'test.dat'); //.
// An instance of the Scp class can also be bound to the SSH connection Scp scp = new Scp; scp.ReuseConnection(ssh); //. // Close the connection ssh.Disconnect; ' Create a new instance of the SecureShellConnection class. Dim ssh As New SecureShellConnection ' Connect to the SSH server. Ssh.Connect(serverName) ' Authenticate ssh.Authenticate(userName, password) ' Create a new instance of the Sftp class ' and bind it to the ssh connection. Dim client As New Sftp client.ReuseConnection(ssh) ' Upload a file. Client.UploadFile('D: data test.dat', 'test.dat') '. ' An instance of the Scp class can also be bound to the SSH connection Dim scp As New Scp scp.ReuseConnection(ssh) '.
' Close the connection ssh.Disconnect.
We need to get about 100 very small files from a remote FTP server using vb.net. Our company won't let us buy (or install) any 3rd party ftp libraries. So we are forced to use something like FtpWebRequest. (Or is there a better free, choice that is already a part of Visual Studio?) This method works, but it is VERY slow.
(I assume because of the constant logging in/out.) Log in with user name and password. Get a file-list from the remote server. Log out Use that file-list to get each file separtely: Log in, get the file, log out.
Log in 99 more times, get each file, log out each time. Instead, we probably should be doing this, but it never works: Log in with user name and password. Get a list of filenames. Download each file. Log out ONCE. We found COUNTLESS examples online of 'getting an FTP file list' and later 'how to download 1 file with FTP'. But we never see 'get EACH file name, and download it NOW'.
Something I just put together, the important part is the fwr.Proxy = Nothing, otherwise it tries to auto get the proxy settings which causes huge delays so setting it to nothing forces it to not use one. If you are using a proxy obviously you need to set this to an actual proxy. Dim fwr As Net.FtpWebRequest = Net.FtpWebRequest.Create(ftpAddress) fwr.Credentials = New NetworkCredential(userName, password) fwr.KeepAlive = True fwr.Method = WebRequestMethods.Ftp.ListDirectory fwr.Proxy = Nothing Try Dim sr As New IO.StreamReader(fwr.GetResponse.GetResponseStream) Dim lst = sr.ReadToEnd.Split(vbNewLine) For Each file As String In lst file = file.Trim 'remove any whitespace If file = '.' OrElse file = '.' Then Continue For Dim fwr2 As Net.FtpWebRequest = Net.FtpWebRequest.Create(ftpAddress & file) fwr2.Credentials = fwr.Credentials fwr2.KeepAlive = True fwr2.Method = WebRequestMethods.Ftp.DownloadFile fwr2.Proxy = Nothing Dim fileSR As New IO.StreamReader(fwr2.GetResponse.GetResponseStream) Dim fileData = fileSR.ReadToEnd fileSR.Close Next sr.Close Catch ex As Exception End Try I know its a bit late but hopefully helps.
Comments are closed.
|
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |