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 FTPAPPE
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 itsItems
text and prepend a line feed to each item string value this way:You can call the method that follows this way:
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 theStreamWriter
: it's set toDefault
→ current Local Codepage.When an Encoding is not specified, it defaults to
UTF8
. Modify as required).If an
Ssl
connection is required, add theImports
and these lines: paste them on top of that method:(The SecurityProtocolType depends on your connection requirements)