作者lls23 (侰侰侁)
看板Visual_Basic
标题Re: [.NET] 请问一个小程式的问题(copy file)...
时间Tue Mar 28 20:18:29 2006
可以用VB的一个控制项
Microsoft Internet Transfer Control
不过可能要注册一把﹐有可能
引用了之後即可使用
其使用方法一般为
采用vb.net的internet传输控件
在窗口中添加控件﹐声明为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(, 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 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
'删除ftp目录
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
※ 引述《kelune (kelune)》之铭言:
: ※ 引述《kelune (kelune)》之铭言:
: : 请问一下....
: : 在dos下 copy c:\abc.txt d:\ 这个指令在vb里要如何表示!?
: : ps. 在win32应用程式(form)的语法...
: 感谢~~已经解决copy to disk 部分~!!
: 若是要copy to FTP 之类的...能否给个方向!?
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 130.88.114.91