Best option for file transfer using VB.Net

~OSD~ 2,201 Reputation points
2022-08-03T13:12:40.87+00:00

Hi

I am in a need to develop VB.Net application with file transfer support from source computer to remote computer.
What would be reliable option here, FTP, SSH (if supported), any example or suggestion(s)?

Developer technologies VB
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Michael Taylor 60,161 Reputation points
    2022-08-03T13:58:09.267+00:00

    Depends upon what you mean by "remote computer". On the same network then use standard File operations and UNC paths. This assumes the user has permissions to both servers. If you need more complex logic then simple file operations then you can also call out to a tool like robocopy.

    If you need to transfer files to a machine across the Internet then that is what (S)FTP is for. .NET Framework supports FTP but .NET Core/6 no longer do. You'll need to use a third party library.

    However FTP is archaic and annoying to work with. There are quite a few file transfer services available online now that have APIs to allow you to better integrate this kind of functionality. However you'll want to look for services that allow you to control what happens when you get files and not just file sharing services as they aren't sufficient here.


  2. Castorix31 90,681 Reputation points
    2022-08-04T12:22:35.19+00:00

    You can use WinSCP

    A test with a random public SFTP server :

     Try  
                Dim sessionOptions As New SessionOptions  
                With sessionOptions  
                    .Protocol = Protocol.Sftp  
                    .HostName = "test.rebex.net"  
                    .UserName = "demo"  
                    .Password = "password"  
                    ' From WinSCP client  
                    .SshHostKeyFingerprint = "ssh-rsa 2048 03:61:c4:98:f1:ff:7d:23:97:51:07:13:88:b8:c5:55"  
                End With  
      
                Using session As New Session  
                    session.Open(sessionOptions)  
                    Dim directory As RemoteDirectoryInfo = session.ListDirectory("/pub/example")  
                    Dim fileInfo As RemoteFileInfo  
                    For Each fileInfo In directory.Files  
                        Console.WriteLine(  
                            "{0} with size {1}, permissions {2} and last modification at {3}",  
                            fileInfo.Name, fileInfo.Length, fileInfo.FilePermissions,  
                            fileInfo.LastWriteTime)  
                    Next  
                End Using  
            Catch ex As Exception  
                Console.WriteLine("Error: {0}", ex)  
            End Try  
    
    0 comments No comments

  3. Lord Marte 20 Reputation points
    2023-01-29T20:57:16.52+00:00

    This is a complete function. Remember to get fingerprint key or use "GiveUpSecurityAndAcceptAny" for public key of secure servers.

    
    'open files
            OpenFileDialog1.FileName = ""
            OpenFileDialog1.Title = "Scegli i files da trasferire in SFTP"
            OpenFileDialog1.Filter = "Tutti i files|*.*"
            If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
                For Each file In OpenFileDialog1.FileNames
                    RichTextBox2.Text &= file & vbCrLf
                Next
            End If
    
    'INVIA
            Try
                Dim sessionOptions As New SessionOptions
                With sessionOptions
                    .Protocol = Protocol.Sftp
                    .HostName = "test.rebex.net" 'port 22
                    .UserName = "demo"
                    .Password = "password"
                    '.SshHostKeyFingerprint = "BNE4ZvsKbhnbnnU9Vd/fgHhpW4+180012oSylyeNJYs"
                    '.SshHostKeyFingerprint = TextBox1.Text
                    '.GiveUpSecurityAndAcceptAnySshHostKey = True
                    .SshHostKeyPolicy = SshHostKeyPolicy.GiveUpSecurityAndAcceptAny
                End With
    
                Using session As New Session
                    'connessione
                    session.Open(sessionOptions)
    
                    'Upload Files
                    Dim transferOptions As New TransferOptions
                    transferOptions.TransferMode = TransferMode.Binary
    
                    Dim lista As String()
                    lista = Split(RichTextBox2.Text, vbCrLf)
    
                    For Each file In lista
                        Dim transferResult As TransferOperationResult
                        'remote folder /home/user/
                        transferResult = session.PutFiles(file, "/home/user/", False, transferOptions)
    
                        'vai avanti su errore
                        transferResult.Check()
    
                        'Stampa
                        For Each transfer In transferResult.Transfers
                            RichTextBox1.Text = ("Trasferimenti di " & transfer.FileName & " eseguito.")
                        Next
                    Next
    
                End Using
    
            Catch ex As Exception
    
            End Try
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.