VB.net连接FTP操作MSDN上vb.netftp类的vb.netftp类 , 看看对你有没有帮助 。GOOD LUCK!
Imports System.Net
Imports System.IO
Module FtpSample
Sub Main(ByVal args() As String)
If args.Length = 0 OrElse args(0).Equals("/?") Then
DisplayUsage()
ElseIf args.Length = 1 Then
Download(args(0))
ElseIf args.Length = 2 Then
If args(0).Equals("/list") Then
List(args(1))
Else
Upload(args(0), args(1))
End If
Else
Console.WriteLine("Unrecognized argument.")
End If
End Sub
Private Sub DisplayUsage()
Console.WriteLine("USAGE:")
Console.WriteLine("FtpSample [/? | FTP download URL | local file")
Console.WriteLine("FTP upload URL | /list FTP list URL]")
Console.WriteLine()
Console.WriteLine("where")
Console.WriteLine("FTP download URLURL of a file to download from an FTP server.")
Console.WriteLine("FTP upload URLLocation on a FTP server to upload a file to.")
Console.WriteLine("FTP list URLLocation on a FTP server to list the contents of.")
Console.WriteLine("local fileA local file to upload to an FTP server.")
Console.WriteLine()
Console.WriteLine("Options:")
Console.WriteLine("/?Display this help message.")
Console.WriteLine("/listSpecifies the list command.")
Console.WriteLine()
Console.WriteLine("EXAMPLES:")
Console.WriteLine("Download a fileFtpSample ")
Console.WriteLine("Upload a fileFtpSample upload.txt ")
End Sub
Private Sub Download(ByVal downloadUrl As String)
Dim responseStream As Stream = Nothing
Dim fileStream As FileStream = Nothing
Dim reader As StreamReader = Nothing
Try
Dim downloadRequest As FtpWebRequest = _
WebRequest.Create(downloadUrl)
Dim downloadResponse As FtpWebResponse = _
downloadRequest.GetResponse()
responseStream = downloadResponse.GetResponseStream()
Dim fileName As String = _
Path.GetFileName(downloadRequest.RequestUri.AbsolutePath)
If fileName.Length = 0 Then
reader = New StreamReader(responseStream)
Console.WriteLine(reader.ReadToEnd())
【vb.netftp类的简单介绍】Else
fileStream = File.Create(fileName)
Dim buffer(1024) As Byte
Dim bytesRead As Integer
While True
bytesRead = responseStream.Read(buffer, 0, buffer.Length)
If bytesRead = 0 Then
Exit While
End If
fileStream.Write(buffer, 0, bytesRead)
End While
End If
Console.WriteLine("Download complete.")
Catch ex As UriFormatException
Console.WriteLine(ex.Message)
Catch ex As WebException
Console.WriteLine(ex.Message)
Catch ex As IOException
Console.WriteLine(ex.Message)
Finally
If reader IsNot Nothing Then
reader.Close()
ElseIf responseStream IsNot Nothing Then
responseStream.Close()
End If
If fileStream IsNot Nothing Then
fileStream.Close()
End If
End Try
End Sub
Private Sub Upload(ByVal fileName As String, ByVal uploadUrl As String)
Dim requestStream As Stream = Nothing
Dim fileStream As FileStream = Nothing
Dim uploadResponse As FtpWebResponse = Nothing
Try
Dim uploadRequest As FtpWebRequest = WebRequest.Create(uploadUrl)
uploadRequest.Method = WebRequestMethods.
' UploadFile is not supported through an Http proxy
' so we disable the proxy for this request.
uploadRequest.Proxy = Nothing
requestStream = uploadRequest.GetRequestStream()
fileStream = File.Open(fileName, FileMode.Open)
Dim buffer(1024) As Byte
Dim bytesRead As Integer
While True
bytesRead = fileStream.Read(buffer, 0, buffer.Length)
If bytesRead = 0 Then
Exit While
End If
requestStream.Write(buffer, 0, bytesRead)
End While
' The request stream must be closed before getting the response.
推荐阅读
- 消防员为什么要直播唱歌,消防员为什么要救火
- js数据结构的书,js数据结构与算法pdf
- html图片内容加a标签,html图片上添加图片
- b站猩猩诺手直播回放,猩猩诺手哪里直播
- go语言向数组最后加元素 go数组底层实现
- 华为鸿蒙通知栏小卡片分割,鸿蒙系统小卡片怎么移出
- 怎么挂双硬盘,怎样装双硬盘视频教程
- 直播间男装素材,男装直播主题
- Java中随机字符代码 java 随机字符串