代码演示VB.NET文件系统对象
创始人
2024-06-10 09:30:32
0

经过长时间学习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  

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...
《非诚勿扰》红人闫凤娇被曝厕所... 【51CTO.com 综合消息360安全专家提醒说,“闫凤娇”、“非诚勿扰”已经被黑客盯上成为了“木...
2012年第四季度互联网状况报... [[71653]]  北京时间4月25日消息,据国外媒体报道,全球知名的云平台公司Akamai Te...