要将文件内容读取到ListBox
,我使用了上一个问题中的DownloadFile
方法
Loading file contents from FTP to ListBox
Dim request As FtpWebRequest =
WebRequest.Create("ftp://example.com/path/Ann.txt")
request.Method = WebRequestMethods.Ftp.DownloadFile
request.Credentials = New NetworkCredential("username", "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
现在我想添加另一个按钮,该按钮使用StreamWriter
和WebRequest.GetRequestStream
将ListBox
的内容上传回FTP。
答案 0 :(得分:2)
你基本上需要我答案的代码 How to download file from FTP and upload it again
虽然要轻松复制ListBox
的内容(行),但您可以稍微修改它以使用StreamWriter
逐行编写:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim request As FtpWebRequest =
WebRequest.Create("ftp://example.com/path/Ann.txt")
request.Method = WebRequestMethods.Ftp.UploadFile
request.Credentials = New NetworkCredential("username", "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
End Sub