• Loading...
Atp UltimateSftp
How to write an application that works with many file system types
Send comments on this topic to ATP, Inc.
Transferring Files between SFTP and other File Systems > How to write an application that works with many file system types

Glossary Item Box

All file system classes derive from the base class FileSystem. This makes it so easy to write an application that works with many file system types such as SFTP, FTP, SCP, ZIP, and Disk file systems. The following example demonstrates how to do that.

C# Copy Code
public void Start()
{
   
// Transfer files to the local disk file system.
   
TransferFiles(DiskFileSystem.Default);
   
// Transfer files to an SFTP file system.
   
Sftp sftp = new Sftp();
   sftp.Connect(
"192.168.126.128", 2222);
   sftp.Authenticate(
"test", "test");
   TransferFiles(sftp);
   sftp.Disconnect();
   
// Transfer files to an SCP file system.
   
Scp scp = new Scp();
   scp.Connect(
"192.168.126.128", 2222);
   scp.Authenticate(
"test", "test");
   TransferFiles(scp);
   scp.Disconnect();
   
// Transfer files to an FTP file system.
   
Ftp ftpsys = new Ftp();
   ftpsys.Connect(
"192.168.126.128", 24);
   ftpsys.Authenticate(
"test", "test");
   TransferFiles(ftpsys);
   ftpsys.Disconnect();
}
void TransferFiles(FileSystem destination)
{
   
// If the destination file system supports directory listing.
   
if ((destination.FileSystemFeatures & FileSystemFeatures.ListDirectory) == FileSystemFeatures.ListDirectory)
   {
       
// Print out files found in the current dir.
       
foreach (IFileInfo f in destination.ListDirectory())
       {
           
// Print out fully qualified name.
           
Console.WriteLine(f.FullName);
           
// If file name matches a search pattern (*.tmp or *.bak), delete it.
           
if (f.Matches(new NameSearchCondition("*.tmp;*.bak")))
               destination.DeleteFile(f.FullName);
       }
       Sftp sftp =
new Sftp();
       
// Connect and authenticate.
       
sftp.Connect("192.168.126.128", 22);
       sftp.Authenticate(
"test", "test");
       
// Copy files from the newly connected SFTP system to the specified destination file system.
       
TransferOptions opt = new TransferOptions(false, false, true, "*.*", FileExistsResolveAction.OverwriteAll, SymlinksResolveAction.Skip, false);
       sftp.DownloadFiles(
"", (IFileInfo[])null, destination, "", opt);
       
       
// Close the connection.
       
sftp.Disconnect();
   }
}
VB.NET Copy Code
Public Sub Start()
    ' Transfer files to the local disk file system.
    TransferFiles(DiskFileSystem.Default)
    ' Transfer files to an SFTP file system.
    Dim sftp As New Sftp()
    sftp.Connect("192.168.126.128", 2222)
    sftp.Authenticate("test", "test")
    TransferFiles(sftp)
    sftp.Disconnect()
    ' Transfer files to an SCP file system.
    Dim scp As New Scp()
    scp.Connect("192.168.126.128", 2222)
    scp.Authenticate("test", "test")
    TransferFiles(scp)
    scp.Disconnect()
    ' Transfer files to an FTP file system.
    Dim ftpsys As New Ftp()
    ftpsys.Connect("192.168.126.128", 24)
    ftpsys.Authenticate("test", "test")
    TransferFiles(ftpsys)
    ftpsys.Disconnect()
End Sub
Private Sub TransferFiles(ByVal destination As FileSystem)
    ' If the destination file system supports directory listing.
    If (destination.FileSystemFeatures And FileSystemFeatures.ListDirectory) = FileSystemFeatures.ListDirectory Then
        ' Print out files found in the current dir.
        For Each f As IFileInfo In destination.ListDirectory()
            ' Print out fully qualified name.
            Console.WriteLine(f.FullName)
            ' If file name matches a search pattern (*.tmp or *.bak), delete it.
            If f.Matches(New NameSearchCondition("*.tmp;*.bak")) Then
                destination.DeleteFile(f.FullName)
            End If
        Next f
        Dim sftp As New Sftp()
        ' Connect and authenticate.
        sftp.Connect("192.168.126.128", 22)
        sftp.Authenticate("test", "test")
        ' Copy files from the newly connected SFTP system to the specified destination file system.
        Dim opt As New TransferOptions(False, False, True, "*.*", FileExistsResolveAction.OverwriteAll, SymlinksResolveAction.Skip, False)
        sftp.DownloadFiles("", CType(Nothing, IFileInfo()), destination, "", opt)
        ' Close the connection.
        sftp.Disconnect()
    End If
End Sub
ATP, Inc. is a Microsoft Partner. Our components are compatible with Windows 7 and optimized for Visual Studio