Can i (Upload / Adding) Listbox Lines To Current Ftp listbox Lines Server Without download Previous ftp server Listbox value.
Instead of Uploading addition listbox lines To Ftp Listbox Lines Server !
Can I simply send listbox new lines and then it Added to currently listbox ftp server lines (without remove old lines, it only add new lines)
With currently Ftp listbox Lines without me having to download and upload the whole ftp listbox?
Example : That's My Code
Download Code (Button 1) [not important]
Dim request As FtpWebRequest =
WebRequest.Create("ftp://test.com/test.txt")
request.Method = WebRequestMethods.Ftp.DownloadFile
request.Credentials = New NetworkCredential("tester1",
"password")
Using response As FtpWebResponse = request.GetResponse(),
stream As Stream = response.GetResponseStream(),
reader As StreamReader = New StreamReader(stream)
While Not reader.EndOfStream
listbox1.Items.Add(reader.ReadLine())
End While
End Using
' Adding the listbox item's Before upload it again'
listbox1.Items.Add(".")
Upload Code (Button 2)
[important to make it direct upload new lines to currently ftp listbox
lines server]
Dim request As FtpWebRequest =
WebRequest.Create("ftp://test.com/test.txt")
request.Method = WebRequestMethods.Ftp.UploadFile
request.Credentials = New NetworkCredential("tester1",
"password")
request.UseBinary = False
Using stream As Stream = request.GetRequestStream(),
writer As StreamWriter = New StreamWriter(stream)
For index As Integer = 0 To listbox1.Items.Count - 1
writer.WriteLine(listbox1.Items(index))
Next
End Using
Catch ex As Exception
Regards
Note: I see you're using Visual Studio 2012. There's the chance that some of the code might not be supported (the .Net version is not specified).
Comment about it if that's the case.
WebRequest
supports the FTP APPE
command for its FtpWebRequest incarnation.
See the WebRequestMethods.Ftp → WebRequestMethods.Ftp.AppendFile.
This method sends an Ftp APPE
command. If the uploaded file exists, it will append the new content.
When writing a text file, you may want to append an Environment.Newline sequence to each line, if/when need.
Since you have a ListBox
control, you can extract its Items
text and prepend a line feed to each item string value this way:
Dim TextLines As String() = listBox1.Items.Cast(Of String)().Select(Function(ln) Environment.NewLine + ln).ToArray()
You can call the method that follows this way:
Dim result As Long = Await FtpAppenAsync("ftp://ftp.server.com/[EntryDir]/[ExistingFile]", TextLines)
Here's a modified method that allows to append some text lines to an existing text file on an FTP Server:
(Take note about the Encoding
of the StreamWriter
: it's set to Default
→ current Local Codepage.
When an Encoding is not specified, it defaults to UTF8
. Modify as required).
Public Async Function FtpAppenAsync(ResourceName As String, TextData As String()) As Task(Of Integer)
Dim request As FtpWebRequest = CType(WebRequest.Create(ResourceName), FtpWebRequest)
request.Credentials = New NetworkCredential("[FtpAccount]", "[FtpPassword]")
request.Method = WebRequestMethods.Ftp.AppendFile
request.UseBinary = True
request.UsePassive = True
Dim TextLinesWritten As Integer = 0
Try
Using ftpStream As Stream = Await request.GetRequestStreamAsync()
Using ftpWriter As New StreamWriter(ftpStream, Encoding.Default)
For Each TextLine As String In TextData
Await ftpWriter.WriteAsync(TextLine)
TextLinesWritten += 1
Console.WriteLine("Uploaded {0} lines", TextLinesWritten)
Next
End Using
End Using
Using response As FtpWebResponse = CType(Await request.GetResponseAsync(), FtpWebResponse)
'Log-Return the StatusCode of the failed upload
If Not (response.StatusCode = FtpStatusCode.ClosingData) Then Return -1
End Using
Catch ex As Exception
'Log/report ex
TextLinesWritten = -1
Throw
End Try
Return TextLinesWritten
End Function
If an Ssl
connection is required, add the Imports
and these lines: paste them on top of that method:
(The SecurityProtocolType depends on your connection requirements)
Imports System.Net.Security
Imports System.Security.Cryptography.X509Certificates
'Method code
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
ServicePointManager.ServerCertificateValidationCallback =
New RemoteCertificateValidationCallback(Function(s, Cert, Chain, sslErrors)
Return True
End Function)
request.EnableSsl = True