直接上传新 Ftp 列表框行
Direct Uploading New Ftp listbox Lines
我可以(上传/添加)列表框行到当前 Ftp 列表框行服务器没有下载以前的 ftp 服务器列表框值。
而不是将添加的列表框行上传到 Ftp 列表框行服务器!
我可以简单地发送列表框新行,然后将其添加到当前列表框 ftp 服务器行(不删除旧行,只添加新行)
当前 Ftp 行列表框无需下载和上传整个 ftp 列表框?
示例:这是我的代码
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
此致
注意:我看到您使用的是 Visual Studio 2012。可能不支持某些代码(未指定 .Net 版本)。
如果是这种情况,请对此发表评论。
WebRequest
支持FTP APPE
command for its FtpWebRequest化身。
参见WebRequestMethods.Ftp → WebRequestMethods.Ftp.AppendFile。
此方法发送 Ftp APPE
命令。如果上传的文件存在,则会追加新的内容。
在编写文本文件时,您可能希望在每一行后附加一个 Environment.Newline 序列,if/when 需要。
因为您有一个 ListBox
控件,您可以提取它的 Items
文本并以这种方式为每个项目字符串值添加一个换行符:
Dim TextLines As String() = listBox1.Items.Cast(Of String)().Select(Function(ln) Environment.NewLine + ln).ToArray()
你可以这样调用后面的方法:
Dim result As Long = Await FtpAppenAsync("ftp://ftp.server.com/[EntryDir]/[ExistingFile]", TextLines)
这是一个修改后的方法,允许将一些文本行附加到 FTP 服务器上的现有文本文件:
(注意 StreamWriter
的 Encoding
:它设置为 Default
→ 当前本地代码页。
如果未指定编码,则默认为 UTF8
。根据需要修改).
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
如果需要 Ssl
连接,请添加 Imports
和这些行:将它们粘贴到该方法的顶部:
(SecurityProtocolType取决于您的连接要求)
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
我可以(上传/添加)列表框行到当前 Ftp 列表框行服务器没有下载以前的 ftp 服务器列表框值。
而不是将添加的列表框行上传到 Ftp 列表框行服务器!
我可以简单地发送列表框新行,然后将其添加到当前列表框 ftp 服务器行(不删除旧行,只添加新行) 当前 Ftp 行列表框无需下载和上传整个 ftp 列表框?
示例:这是我的代码
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
此致
注意:我看到您使用的是 Visual Studio 2012。可能不支持某些代码(未指定 .Net 版本)。
如果是这种情况,请对此发表评论。
WebRequest
支持FTP APPE
command for its FtpWebRequest化身。
参见WebRequestMethods.Ftp → WebRequestMethods.Ftp.AppendFile。
此方法发送 Ftp APPE
命令。如果上传的文件存在,则会追加新的内容。
在编写文本文件时,您可能希望在每一行后附加一个 Environment.Newline 序列,if/when 需要。
因为您有一个 ListBox
控件,您可以提取它的 Items
文本并以这种方式为每个项目字符串值添加一个换行符:
Dim TextLines As String() = listBox1.Items.Cast(Of String)().Select(Function(ln) Environment.NewLine + ln).ToArray()
你可以这样调用后面的方法:
Dim result As Long = Await FtpAppenAsync("ftp://ftp.server.com/[EntryDir]/[ExistingFile]", TextLines)
这是一个修改后的方法,允许将一些文本行附加到 FTP 服务器上的现有文本文件:
(注意 StreamWriter
的 Encoding
:它设置为 Default
→ 当前本地代码页。
如果未指定编码,则默认为 UTF8
。根据需要修改).
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
如果需要 Ssl
连接,请添加 Imports
和这些行:将它们粘贴到该方法的顶部:
(SecurityProtocolType取决于您的连接要求)
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