Visual_Basic 板


LINE

可以用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







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:iOS站内搜寻

TOP