代码演示VB.NET文件系统对象

经过长时间学习VB.NET文件系统对象,于是和大家分享一下,看完本文你肯定有不少收获,希望本文能教会你更多东西。我们编程经常和VB.NET文件系统对象,比如获取硬盘的剩余空间、判断文件夹或文件是否存在等。在VB.NET文件系统对象(File System Object)没有推出以前,完成这些功能需要调用 Windows API 函数或者使用一些比较复杂的过程来实现,使编程复杂、可靠性差又容易出错。

站在用户的角度思考问题,与客户深入沟通,找到沙湾网站设计与沙湾网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:网站制作、成都网站建设、企业官网、英文网站、手机端网站、网站推广、域名与空间、网站空间、企业邮箱。业务覆盖沙湾地区。

#T#使用 Windows 提供的的文件系统对象,一切变得简单多了。以下笔者举出一些编程中比较常用的例子,以函数或过程的形式提供给大家,读者可在编程中直接使用,也可以改进后实现更为强大的功能。要应用 FSO 对象,须要引用一个名为 Scripting 的类型库,方法是,执行 VB6.0 的菜单项“工程/引用”,添加引用列表框中的“Microsoft Scripting Runtime”一项。然后我们在“对象浏览器”中就可以看到 Scripting 类型库下的众多对象及其方法、属性。

1、判断光驱的盘符

 
 
  1. Function GetCDROM() ' 返回光驱的盘符(字母)  
  2. Dim Fso As New FileSystemObject '创建 FSO 对象的一个实例  
  3. Dim FsoDrive As Drive, FsoDrives As Drives '定义驱动器、驱动器集合对象  
  4. Set FsoFsoDrives = Fso.Drives  
  5. For Each FsoDrive In FsoDrives '遍历所有可用的驱动器  
  6. If FsoDrive.DriveType = CDRom Then '如果驱动器的类型为 CDrom  
  7. GetCDROM = FsoDrive.DriveLetter '输出其盘符  
  8. Else  
  9. GetCDROM = "" 
  10. End If  
  11. Next  
  12. Set Fso = Nothing 
  13. Set FsoDrive = Nothing 
  14. Set FsoDrives = Nothing 
  15. End Function 

2、判断文件、文件夹是否存在

 
 
  1. '返回布尔值:True 存在,False 不存在,filername 文件名  
  2. Function FileExist(filename As String)   
  3. Dim Fso As New FileSystemObject  
  4. If Fso.FileExists(filename) = True Then  
  5. FileExist = True 
  6. Else  
  7. FileExist = False 
  8. End If  
  9. Set Fso = Nothing 
  10.  
  11. End Function  
  12. '返回布尔值:True 存在,False 不存在,foldername 文件夹  
  13. Function FolderExist(foldername As String)  
  14. Dim Fso As New FileSystemObject  
  15. If Fso.FolderExists(foldername) = True Then  
  16.  
  17. FolderExist = True 
  18. Else  
  19. FolderExist = False 
  20. End If  
  21. Set Fso = Nothing 
  22. End Function  

3、获取驱动器参数:

 
 
  1. '返回磁盘总空间大小(单位:M),Drive = 盘符 A ,C, D ...  
  2. Function AllSpace(Drive As String)  
  3. Dim Fso As New FileSystemObject, Drv As Drive  
  4.  Set Drv = Fso.GetDrive(Drive) '得到 Drv 对象的实例  
  5. If Drv.IsReady Then '如果该驱动器存在(软驱或光驱里有盘片,硬盘存取正常)  
  6. AllSpace = Format(Drv.TotalSize / (2 ^ 20), "0.00") '将字节转换为兆  
  7. Else  
  8. AllSpace = 0 
  9. End If  
  10. Set Fso = Nothing 
  11. Set Drv = Nothing 
  12. End Function  
  13. '返回磁盘可用空间大小(单位:M),Drive = 盘符 A ,C, D ...  
  14. Function FreeSpace(drive)  
  15. Dim Fso As New FileSystemObject, drv As drive  
  16. Set drv = Fso.GetDrive(drive)  
  17. If drv.IsReady Then  
  18. FreeSpace = Format(drv.FreeSpace / (2 ^ 20), "0.00")  
  19. End If  
  20. Set Fso = Nothing 
  21. Set Drv = Nothing 
  22. End Function  
  23.  
  24. '获取驱动器文件系统类型,Drive = 盘符 A ,C, D ...  
  25. Function FsType(Drive As String)  
  26. Dim Fso As New FileSystemObject, Drv As Drive  
  27. Set Drv = Fso.GetDrive(Drive)  
  28. If Drv.IsReady Then  
  29.  
  30. FsType = Drv.FileSystem  
  31. Else  
  32. FsType = "" 
  33. End If  
  34. Set Fso = Nothing 
  35. Set Drv = Nothing 
  36. End Function  

4,获取系统文件夹路径

 
 
  1. '返回 Windows 文件夹路径  
  2. Function GetWindir()  
  3. Dim Fso As New FileSystemObject  
  4. GetWindir = Fso.GetSpecialFolder(WindowsFolder)  
  5. Set Fso = Nothing 
  6. End Function  
  7. '返回 Windows\System 文件夹路径  
  8. Function GetWinSysdir()  
  9. Dim Fso As New FileSystemObject  
  10. GetWinSysdir = Fso.GetSpecialFolder(SystemFolder)  
  11. Set Fso = Nothing 
  12. End Function 

5,综合运用:一个文件备份通用过程

 
 
  1. 'Filename = 文件名,Drive = 驱动器,Folder = 文件夹(一层)  
  2. Sub BackupFile(Filename As String, Drive As String, Folder As String)  
  3. Dim Fso As New FileSystemObject '创建 FSO 对象实例  
  4. Dim Dest_path As String, Counter As Long  
  5. Counter = 0 
  6. Do While Counter < 6 '如果驱动器没准备好,继续检测。共检测 6 秒  
  7. CounterCounter = Counter + 1  
  8. Call Waitfor(1) '间隔 1 秒  
  9.  
  10. If Fso.Drives(Drive).IsReady = True Then  
  11. Exit Do  
  12. End If  
  13. Loop  
  14. If Fso.Drives(Drive).IsReady = False Then '6 秒后目标盘仍未准备就绪,退出  
  15.  
  16. MsgBox " 目标驱动器 " & Drive & " 没有准备好! ", vbCritical  
  17. Exit Sub  
  18. End If  
  19. If Fso.GetDrive(Drive).FreeSpace < Fso.GetFile(Filename).Size Then  
  20. MsgBox "目标驱动器空间太小!", vbCritical '目标驱动器空间不够,退出  
  21. Exit Sub  
  22. End If  
  23. If Right(Drive, 1) <> ":" Then  
  24. DriveDrive = Drive & ":"  
  25. End If  
  26. If Left(Folder, 1) <> "\" Then  
  27. Folder = "\" & Folder  
  28. End If  
  29. If Right(Folder, 1) <> "\" Then  
  30. FolderFolder = Folder & "\"  
  31. End If  
  32. Dest_path = Drive & Folder  
  33. If Not Fso.FolderExists(Dest_path) Then '如果目标文件夹不存在,创建之  
  34. Fso.CreateFolder Dest_path  
  35. End If  
  36. Fso.CopyFile Filename, Dest_path & Fso.GetFileName(Filename), True  
  37. '拷贝,直接覆盖同名文件  
  38. MsgBox " 文件备份完毕。", vbOKOnly  
  39. Set Fso = Nothing 
  40. End Sub  
  41. Private Sub Waitfor(Delay As Single) '延时过程,Delay 单位约为 1 秒  
  42. Dim StartTime As Single  
  43. StartTime = Timer 
  44. Do Until (Timer - StartTime) > Delay  
  45. Loop  
  46. End Sub  

网页题目:代码演示VB.NET文件系统对象
转载源于:http://www.shufengxianlan.com/qtweb/news26/136276.html

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

广告

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