代码演示VB.NET处理FTP两种方法

熟悉VB.NET都知道,它没有专门处理FTP的类,现在我们可以通过调用系统自带的FTP.EXE或者是调用win32API中的wininet.dll两种方法来完成基本操作。希望以下的代码能为大家抛砖引玉。

公司主营业务:网站建设、成都网站建设、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。创新互联建站是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。创新互联建站推出丰泽免费做网站回馈大家。

VB.NET处理FTP方法一:使用Ftp.exe,通过process类来调用它。

 
 
 
  1. ImportsSystem.Diagnostics  
  2. ...  
  3. PublicSubGetFileByCallFtp()  
  4. '定义ProcessStartInfo,Process的启动信息。  
  5. DimpsiAsNewProcessStartInfo  
  6. 'ftp.exe的路径***放到配置文件里。  
  7. psi.FileName="C:\WINNT\system32\ftp.exe" 
  8. psi.RedirectStandardInput=False 
  9. psi.RedirectStandardOutput=True 
  10. '该值指示不使用操作系统Shell程序启动进程。  
  11. psi.UseShellExecute=False 
  12. '命令集文件名.注意,路径中不能有空格.  
  13. DimfileNameAsString="C\ftp.txt" 
  14. '-s:FileName表示,从文件中读取控制命令  
  15. psi.Arguments="-s:"+fileName  
  16. DimprocAsProcess  
  17. proc=Process.Start(psi)  
  18. '等待进程完成任务  
  19. proc.WaitForExit()  
  20. '在控制台输出结果  
  21. Console.WriteLine(proc.StandardOutput)  
  22. Console.ReadLine()  
  23. EndSub  

VB.NET处理FTP方法二,使用win32api——wininet.dll

首先是,api声明:

因为此测试程序,是VB.NETConsoleApplication所以,api声明写在Module里,

方法是静态的。所以没加Shared关键字,这一点请大家注意。

 
 
 
  1. ("wininet")>_  
  2. PublicFunctionInternetOpen(ByValsAgentAsString,ByValLAccessTypeAsInteger,ByValsProxyNameAsString,_  
  3. ByValSProxyBypassAsString,ByVallFlagsAsInteger)AsInteger  
  4. EndFunction  
  5. ("wininet")>_  
  6. PublicFunctionInternetConnect(ByValhInternetSessionAsInteger,ByValsServerNameAsString,_  
  7. ByValnServerPortAsInteger,ByValsUsernameAsString,_  
  8. ByValsPasswordAsString,ByVallServiceAsInteger,_  
  9. ByVallFlagsAsInteger,ByVallContextAsInteger)AsInteger  
  10. EndFunction  
  11. ("wininet")>_  
  12. PublicFunctionFtpGetFile(ByValhFtpSessionAsInteger,ByVallpszRemoteFileAsString,_  
  13. ByVallpszNewFileAsString,ByValfFailIfExistsAsBoolean,_  
  14. ByValdwFlagsAndAttributesAsInteger,ByValdwFlagsAsInteger,_  
  15. ByValdwContextAsInteger)AsBoolean  
  16. EndFunction  
  17. ("wininet")>_  
  18. PublicFunctionInternetCloseHandle(ByValhInetAsInteger)AsInteger  
  19. EndFunction 

调用:

 
 
 
  1. PublicSubGetFileByCallWininetDLL()  
  2. Try  
  3. DimintinetAsInteger=InternetOpen(Nothing,0,Nothing,Nothing,0)  
  4. Ifintinet>0Then  
  5. '参数:intinet的session值,ftp地址,端口,用户名,密码,lService,lFlags,lContext  
  6. DimintinetconnAsInteger=InternetConnect(intinet,"192.168.110.152",0,"tokiwa","tokiwa",1,0,0)  
  7. Ifintinetconn>0Then  
  8. '下载某个文件到指定文件  
  9. DimretAsBoolean=FtpGetFile(intinetconn,"pagerror.gif","C:\itest.gif",0,0,1,0)  
  10. IfretThen  
  11. Console.WriteLine("ok!")  
  12. Console.ReadLine()  
  13. EndIf  
  14. InternetCloseHandle(intinetconn)  
  15. InternetCloseHandle 

以上是VB.NET处理FTP的两种方法,大家学会了吗?

【编辑推荐】

  1. VB.NET实例教程对关于Array问题的解决
  2. VB.NET实例教程之查询系统启动信息案例剖析
  3. VB.NET实用教程之VB.NET实现登陆Ftp经典方案讲解
  4. VB.NET实用教程之抓取网页乱码解决方案
  5. 突破VB.NET命名空间两大难关

网页标题:代码演示VB.NET处理FTP两种方法
网页地址:http://www.zyruijie.cn/qtweb/news10/5860.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联