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

开发 后端
这里介绍了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  
责任编辑:田树 来源: 博客
相关推荐

2009-10-29 16:29:02

VB.NET文件系统对

2009-11-02 09:21:04

VB.NET文件系统

2009-10-27 10:58:00

VB.NET文件名排序

2009-11-03 11:06:40

VB.NET事件

2009-10-29 13:46:14

VB.NET DES加

2009-10-28 15:18:46

VB.NET网络应用

2010-01-21 16:17:32

VB.NET文件对象

2010-01-20 13:42:10

VB.NET访问INIGetPrivateP

2009-10-27 16:36:46

VB.NET文件流

2009-10-14 13:21:46

VB.NET Acco

2009-10-26 10:30:57

VB.NET处理FTP

2009-10-26 14:50:18

VB.NET遍历注册表

2010-01-15 10:05:35

VB.NET文件对象

2009-10-26 09:50:20

VB.NET Star

2009-10-09 15:59:41

VB.NET对象

2009-10-26 11:04:36

VB.NET UDP协

2009-10-27 14:05:59

VB.NET程序

2010-01-11 11:37:08

VB.NET操作CSV

2009-10-13 17:03:55

VB.NET面向对象

2009-11-02 16:22:16

VB.NET面向对象
点赞
收藏

51CTO技术栈公众号