0

My objective is to create a csv and sftp it without saving the file on the local machine using vb.net. Here is my create csv code:

 Public Sub writeCSV()

     Dim headers = (From header As DataGridViewColumn In 

     DataGridView1.Columns.Cast(Of DataGridViewColumn)() _
          Select header.HeaderText).ToArray

     Dim rows = From row As DataGridViewRow In DataGridView1.Rows.Cast(Of 

    DataGridViewRow)() _
               Where Not row.IsNewRow _
               Select Array.ConvertAll(row.Cells.Cast(Of 

    DataGridViewCell).ToArray, Function(c) If(c.Value IsNot Nothing,

    c.Value.ToString, ""))

    Using sw As New IO.StreamWriter("foo.csv")

        sw.WriteLine(String.Join(",", headers))

        For Each r In rows

            sw.WriteLine(String.Join(",", r))

        Next

    End Using

    'Process.Start("foo.csv")

    'SFTP("foo.csv")
End Sub

Instead of starting and/or saving the process.. I'd like to write a sub that i can just sftp this foo.csv to a vendor's server. Is this possible? Also, the SFTP will need to use a key not a password.

Thanks in advance,

Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
Matt
  • 37
  • 1
  • 8
  • have you worked out yet how to SFTP a normal file and get that working? Since you are making the point about needing a key, I suspect not. There are lots of client libraries available that will do this - you need to choose one and write the appropriate code for it. Then worry about whether you can do it without saving the file first. If not, you can just delete the file from your machine once the transfer completes. It looks like using raw FTP you can do it: http://stackoverflow.com/questions/39224938/upload-a-file-to-an-ftp-server-from-a-string-or-stream so maybe SFTP will work too – ADyson Nov 29 '16 at 16:07
  • Thanks ADyson. Yes, I have worked out SFTP; and as you pointed out deleting the file after the sftp is a good alternative; however, in this instance that is not a preferred option. I'd like the file object sent after creation which I've never done. – Matt Nov 29 '16 at 16:18

2 Answers2

0

The following code uses edtFTPnet/PRO, which I co-developed, to do the task you need:

Sub SendCsvToSFTP(serverAddress As String, userName As String, password As String, fileName As String, csvText As String)

    Using sftp As New EnterpriseDT.Net.Ftp.SecureFTPConnection

        ' Set up the SFTP connection
        sftp.ServerAddress = serverAddress
        sftp.Protocol = EnterpriseDT.Net.Ftp.FileTransferProtocol.SFTP
        sftp.UserName = userName
        sftp.Password = password

        ' Connect to the server
        sftp.Connect()

        Using csvStream As New MemoryStream()
            Using csvWriter As New StreamWriter(csvStream)
                csvWriter.Write(csvText)                    ' Write the CSV text to the writer
                csvWriter.Flush()                           ' Make sure it's all written to the underlying stream
                csvStream.Seek(0, SeekOrigin.Begin)         ' Now wind back to the beginning before uploading

                sftp.UploadStream(csvStream, fileName)      ' Upload the stream
            End Using
        End Using

        ' Close the SFTP connection
        sftp.Close()
    End Using

End Sub

As you can see, it uses the SecureFTPConnection class to do the work. Note that edtFTPnet/PRO is a commercial product, but there's a 30-day trial available.

HansA
  • 1,375
  • 2
  • 11
  • 19
  • Thank you so much! – Matt Dec 20 '16 at 15:14
  • @Matt If this answers the question, then "thanks" on SO would be to click the checkmark. That will remove this from the Unanswered list and help others find good answers when they search. the [Tour] is very brief and explains more about How SO Works. – Ňɏssa Pøngjǣrdenlarp Jan 25 '17 at 21:50
-1

Create your CSV file as a memorystream then send that memorystream to your FTP Server.

Here is a link showing the sending of a memorystream to an FTP server: ASP.NET C# upload MemoryStream content via FTPwebRequest issue

Community
  • 1
  • 1
Ibrahim Malluf
  • 657
  • 4
  • 6
  • Thank you! I'll try that – Matt Nov 29 '16 at 19:21
  • FTP and SFTP are entirely different protocols. I would be surprised if the FTPWebRequest object can make a transfer over SFTP. Also, link-only answers are usually discouraged by the moderators. – ADyson Nov 30 '16 at 09:38
  • additionally - I was not able to get this working. I did try making it a memorystream: – Matt Nov 30 '16 at 16:53