一个好老好老的老程序员了。
全部博文(915)
分类:
2008-08-14 11:24:56
在窗口中添加控件,声明为axlnet1
初始化传输控件
With AxInet1
.URL = "ftp://192.168.3.38"
.UserName = "WYX"
.Password = "WYX"
.Protocol = InetCtlsObjects.ProtocolConstants.icFTP
.RequestTimeout = 120
利用初始化了的控件进行文件操作
Public Function delFtpFile(ByVal ftpPath As String, ByVal ftpFileNameArray() As String) As Boolean
'判断ftpPath和ftpFileNameArray() 都不能为空,如果为空false
If (ftpPath = Nothing) Then
Return False
ElseIf ftpPath.Trim.Equals("") Then
Return False
End If
If (ftpFileNameArray Is Nothing) Then
Return False
ElseIf ftpFileNameArray.Length = 0 Then
Return False
End If
'执行删除操作
Dim iNum As Integer
Dim i As Integer
Dim strExe As String
iNum = ftpFileNameArray.Length
For i = 0 To iNum - 1
If ftpFileNameArray(i) = Nothing Then
i = i + 1
End If
If i > iNum - 1 Then
Exit For
End If
strExe = "delete " + ftpPath.Trim + "\" + ftpFileNameArray(i)
Try
Me.AxInet1.Execute(, strExe)
Catch ex As Exception
Throw ex
''如果一个文件删除失败,重新进行删除操作
'If (MsgBox(ex.Message + "char(13)删除文件" + ftpFileNameArray(i) + "失败,是否重试?", MsgBoxStyle.OKCancel) = MsgBoxResult.OK) Then
' Dim j As Integer
' Dim bret As Boolean
' Dim icount As Integer = 0
' Dim myStrArr(iNum - 1 - i) As String
' For j = i To iNum - 1
' myStrArr(icount) = ftpFileNameArray(j)
' Next
' bret = delFtpFile(ftpPath, myStrArr)
' Return bret
'Else
' Return False
'End If
End Try
'等待ftp控件完成删除操作然后再返回true
stillExecutingStatus()
Next
Return True
End Function
Public Function getFtpFile(ByVal ftpPath As String, ByVal localPath As String, ByVal ftpFileNameArray() As String) As Boolean
'判断ftpPath、localPath和ftpFileNameArray()是否为空,如果为空返回false
If (ftpPath = Nothing) Then
Return False
ElseIf ftpPath.Trim.Equals("") Then
Return False
End If
If (localPath = Nothing) Then
Return False
ElseIf localPath.Trim.Equals("") Then
Return False
End If
If (ftpFileNameArray Is Nothing) Then
Return False
ElseIf ftpFileNameArray.Length = 0 Then
Return False
End If
Dim iNum As Integer
Dim i As Integer
Dim strExe As String
iNum = ftpFileNameArray.Length
For i = 0 To iNum - 1
If ftpFileNameArray(i) = Nothing Then
i = i + 1
End If
If i > iNum - 1 Then
Exit For
End If
strExe = "get " + ftpPath.Trim + "\" + ftpFileNameArray(i) + " " + localPath + "\" + ftpFileNameArray(i)
Try
Me.AxInet1.Execute(, strExe)
Catch ex As Exception
'如果下载失败删除本地已下载的文件
Dim localfilename(i) As String
Dim j As Integer
For j = 0 To i - 1
localfilename(j) = ftpFileNameArray(j)
Next
Me.delLocalDir(localPath, localfilename)
Throw ex
'如果一个文件下载失败,从失败的文件处重新进行下载操作
'If (MsgBox(ex.Message + "char(13)下载文件" + ftpFileNameArray(i) + "失败,是否重试?", MsgBoxStyle.OKCancel) = MsgBoxResult.OK) Then
' Dim j As Integer
' Dim bret As Boolean
' Dim icount As Integer = 0
' Dim myStrArr(iNum - 1 - i) As String
' For j = i To iNum - 1
' myStrArr(icount) = ftpFileNameArray(j)
' Next
' bret = getFtpFile(ftpPath, localPath, myStrArr)
' Return bret
'Else
' Return False
'End If
End Try
'等待ftp控件完成删除操作然后再返回true
stillExecutingStatus()
Next
Return True
End Function
Public Function putFtpFile(ByVal ftpPath As String, ByVal localPath As String, ByVal ftpFileNameArray() As String, ByVal localFileNameArray() As String) As Boolean
'判断ftpPath、localPath和ftpFileNameArray()是否为空,如果为空返回false
If (ftpPath = Nothing) Then
Return False
ElseIf ftpPath.Trim.Equals("") Then
Return False
End If
If (localPath = Nothing) Then
Return False
ElseIf localPath.Trim.Equals("") Then
Return False
End If
If (ftpFileNameArray Is Nothing) Then
Return False
ElseIf ftpFileNameArray.Length = 0 Then
Return False
End If
If (localFileNameArray Is Nothing) Then
Return False
ElseIf localFileNameArray.Length = 0 Then
Return False
End If
'执行上传操作
Dim iNum As Integer
Dim i As Integer
Dim strExe As String
iNum = ftpFileNameArray.Length
For i = 0 To iNum - 1
If ftpFileNameArray(i) = Nothing Then
i = i + 1
End If
If i > iNum - 1 Then
Exit For
End If
strExe = "put " + localPath.Trim + "\" + localFileNameArray(i) + " " + ftpPath + "\" + ftpFileNameArray(i)
Try
Me.AxInet1.Execute(AxInet1.URL, strExe,vbNullString,vbNullString) '这里应该是四个参数
Catch ex As Exception
Throw ex
''如果一个文件下载失败,从失败的文件处重新进行上传操作
'If (MsgBox(ex.Message + "char(13)上传文件" + ftpFileNameArray(i) + "失败,是否重试?", MsgBoxStyle.OKCancel) = MsgBoxResult.OK) Then
' Dim j As Integer
' Dim bret As Boolean
' Dim icount As Integer = 0
' Dim myStrArrftp(iNum - 1 - i) As String
' Dim myStrArrlocal(iNum - 1 - i) As String
' For j = i To iNum - 1
' myStrArrftp(icount) = ftpFileNameArray(j)
' myStrArrlocal(icount) = localFileNameArray(j)
' Next
' bret = putFtpFile(ftpPath, localPath, myStrArrftp, myStrArrlocal)
' Return bret
'Else
' Return False
'End If
End Try
Dim icnt As Integer = 0
'等待ftp控件完成删除操作然后再返回true
stillExecutingStatus()
Next
Return True
End Function
Public Function delFtpDir(ByVal ftpPath As String, ByVal ftpFileNameArray() As String) As Boolean
'先删除目录里面的文件,然后再删除目录
If Me.delFtpFile(ftpPath, ftpFileNameArray) = True Then
Me.AxInet1.Execute(, "rmdir " + ftpPath)
'等待ftp控件完成删除操作然后再返回true
stillExecutingStatus()
Return True
Else
'如果文件夹里面没有文件删除文件夹
If ftpPath = Nothing Then
Return False
ElseIf ftpPath.Trim.Equals("") Then
Return False
Else
Me.AxInet1.Execute(, "rmdir " + ftpPath)
'等待ftp控件完成删除操作然后再返回true
stillExecutingStatus()
Return True
End If
End If
End Function
'创建ftp目录,并上传文件,可以进行覆盖操作
Public Function crtFtpDir(ByVal ftpPath As String, ByVal localPath As String, ByVal ftpFileNameArray() As String, ByVal localFileNameArray() As String) As Boolean
'创建目录
Me.AxInet1.Execute(, "mkdir " + ftpPath)
'等待ftp控件完成删除操作然后再返回true
stillExecutingStatus()
Return Me.putFtpFile(ftpPath, localPath, ftpFileNameArray, localFileNameArray)
'上传文件
End Function
'ftp上文件改名(单个文件)
''???有必要批量修改文件吗?
Public Function renameFtpFile(ByVal ftpPath As String, ByVal OriginalFtpFilename As String, ByVal newFtpFilename As String) As Boolean
'1下载文件到本地目录,2 上传文件(更改名称) 3 删除本地文件 4 删除ftp文件
Try
'下载文件到本地
Dim localPath As String
localPath = "c:\tempwjbphotosoftware"
'创建本地临时目录
System.IO.Directory.CreateDirectory(localPath)
Me.AxInet1.Execute(, "get " + ftpPath + "\" + OriginalFtpFilename + localPath + "\" + OriginalFtpFilename)
'等待ftp控件完成删除操作然后再返回true
stillExecutingStatus()
'上传文件
Me.AxInet1.Execute(, "put " + localPath + "\" + OriginalFtpFilename + " " + ftpPath + "\" + newFtpFilename)
stillExecutingStatus()
'删除ftp原文件
Me.AxInet1.Execute(, " delete " + ftpPath + "\" + OriginalFtpFilename)
stillExecutingStatus()
'删除本地文件
System.IO.Directory.Delete(localPath)
Return True
Catch ex As Exception
'Return False
Throw ex
End Try
End Function
'移动单个文件,前提目标文件夹已经存在
Public Function moveFtpFile(ByVal originalFtpPath As String, ByVal newFtpPath As String, ByVal OriginalFtpFilename As String, ByVal newFtpFilename As String) As Boolean
''1下载文件到本地目录,2 上传文件(更改名称) 3 删除本地文件 4 删除ftp文件
Try
'下载文件到本地
Dim localPath As String
localPath = "c:\tempwjbphotosoftware"
'创建本地临时目录
System.IO.Directory.CreateDirectory(localPath)
Me.AxInet1.Execute(, "get " + originalFtpPath + "\" + OriginalFtpFilename + localPath + "\" + OriginalFtpFilename)
'等待ftp控件完成删除操作然后再返回true
stillExecutingStatus()
Me.AxInet1.Execute(, "mkdir " + newFtpPath)
stillExecutingStatus()
'上传文件
Me.AxInet1.Execute(, "put " + localPath + "\" + OriginalFtpFilename + " " + newFtpPath + "\" + newFtpFilename)
stillExecutingStatus()
'删除ftp原文件
Me.AxInet1.Execute(, " delete " + originalFtpPath + "\" + OriginalFtpFilename)
stillExecutingStatus()
'删除本地文件
Dim localfilename() As String = New String(0) {OriginalFtpFilename}
Me.delLocalDir(localPath, localfilename)
Return True
Catch ex As Exception
Throw ex
'Return False
End Try
End Function
' 移动整个文件夹,做法:把文件都下载到本地,在ftp上创建文件夹,上传,然后再删掉原来的目录
Public Function moveFtpDir(ByVal originalFtpPath As String, ByVal newFtpPath As String) As Boolean
'判断originalftppath是否为空
If originalFtpPath = Nothing Then
Return False
ElseIf originalFtpPath.Trim.Equals("") Then
Return False
End If
'判断newFtpPath是否为空
If newFtpPath = Nothing Then
Return False
ElseIf newFtpPath.Trim.Equals("") Then
Return False
End If
Me.AxInet1.Execute(, "rename " + originalFtpPath + " " + newFtpPath)
stillExecutingStatus()
Return True
End Function
'删除本地目录
Public Sub delLocalDir(ByVal localPath As String, ByVal localfilename() As String)
Try
If localfilename Is Nothing Then
'删除空目录
System.IO.Directory.Delete(localPath)
ElseIf localfilename.Length = 0 Then
'删除空目录
System.IO.Directory.Delete(localPath)
Else
'清空目录
Dim i As Integer
For i = 0 To localfilename.Length - 1
System.IO.File.Delete(localPath + "\" + localfilename(i))
Next
'删除空目录
System.IO.Directory.Delete(localPath)
End If
Catch ex As Exception
Throw ex
End Try
End Sub
'等待stillexecuting状态为false
Public Sub stillExecutingStatus()
While (Me.AxInet1.StillExecuting)
System.Windows.Forms.Application.DoEvents()
End While
End Sub
前边提到的数组都是要操作的文件名的数组。
(上传文件已经经过实践,没有问题,可以通过控件的responsecode和responseinfo属性来判断文件上传成功与否,在使用前需要引入MSINET.OCX,参照下图)