自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

代碼演示VB.NET文件系統(tǒng)對象

開發(fā) 后端
這里介紹了VB.NET文件系統(tǒng)對象,文章舉出了一些編程中常用的例子,以函數(shù)或過程的形式提供給大家,希望對大家有幫助。

經(jīng)過長時間學(xué)習(xí)VB.NET文件系統(tǒng)對象,于是和大家分享一下,看完本文你肯定有不少收獲,希望本文能教會你更多東西。我們編程經(jīng)常和VB.NET文件系統(tǒng)對象,比如獲取硬盤的剩余空間、判斷文件夾或文件是否存在等。在VB.NET文件系統(tǒng)對象(File System Object)沒有推出以前,完成這些功能需要調(diào)用 Windows API 函數(shù)或者使用一些比較復(fù)雜的過程來實現(xiàn),使編程復(fù)雜、可靠性差又容易出錯。

#T#使用 Windows 提供的的文件系統(tǒng)對象,一切變得簡單多了。以下筆者舉出一些編程中比較常用的例子,以函數(shù)或過程的形式提供給大家,讀者可在編程中直接使用,也可以改進后實現(xiàn)更為強大的功能。要應(yīng)用 FSO 對象,須要引用一個名為 Scripting 的類型庫,方法是,執(zhí)行 VB6.0 的菜單項“工程/引用”,添加引用列表框中的“Microsoft Scripting Runtime”一項。然后我們在“對象瀏覽器”中就可以看到 Scripting 類型庫下的眾多對象及其方法、屬性。

1、判斷光驅(qū)的盤符

  1. Function GetCDROM() ' 返回光驅(qū)的盤符(字母)  
  2. Dim Fso As New FileSystemObject '創(chuàng)建 FSO 對象的一個實例  
  3. Dim FsoDrive As Drive, FsoDrives As Drives '定義驅(qū)動器、驅(qū)動器集合對象  
  4. Set FsoFsoDrives = Fso.Drives  
  5. For Each FsoDrive In FsoDrives '遍歷所有可用的驅(qū)動器  
  6. If FsoDrive.DriveType = CDRom Then '如果驅(qū)動器的類型為 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 存在,F(xiàn)alse 不存在,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 存在,F(xiàn)alse 不存在,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、獲取驅(qū)動器參數(shù):

  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 '如果該驅(qū)動器存在(軟驅(qū)或光驅(qū)里有盤片,硬盤存取正常)  
  6. AllSpace = Format(Drv.TotalSize / (2 ^ 20), "0.00") '將字節(jié)轉(zhuǎn)換為兆  
  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. '獲取驅(qū)動器文件系統(tǒng)類型,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,獲取系統(tǒng)文件夾路徑

  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 = 驅(qū)動器,Folder = 文件夾(一層)  
  2. Sub BackupFile(Filename As String, Drive As String, Folder As String)  
  3. Dim Fso As New FileSystemObject '創(chuàng)建 FSO 對象實例  
  4. Dim Dest_path As String, Counter As Long  
  5. Counter = 0 
  6. Do While Counter < 6 '如果驅(qū)動器沒準備好,繼續(xù)檢測。共檢測 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 " 目標驅(qū)動器 " & Drive & " 沒有準備好! ", vbCritical  
  17. Exit Sub  
  18. End If  
  19. If Fso.GetDrive(Drive).FreeSpace < Fso.GetFile(Filename).Size Then  
  20. MsgBox "目標驅(qū)動器空間太??!", vbCritical '目標驅(qū)動器空間不夠,退出  
  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 '如果目標文件夾不存在,創(chuàng)建之  
  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  
責(zé)任編輯:田樹 來源: 博客
相關(guān)推薦

2009-10-29 16:29:02

VB.NET文件系統(tǒng)對

2009-11-02 09:21:04

VB.NET文件系統(tǒng)

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網(wǎng)絡(luò)應(yīng)用

2010-01-21 16:17:32

VB.NET文件對象

2009-10-26 14:50:18

VB.NET遍歷注冊表

2009-10-26 10:30:57

VB.NET處理FTP

2009-10-26 09:50:20

VB.NET Star

2010-01-20 13:42:10

VB.NET訪問INIGetPrivateP

2009-10-14 13:21:46

VB.NET Acco

2009-10-27 16:36:46

VB.NET文件流

2009-10-09 15:59:41

VB.NET對象

2010-01-15 10:05:35

VB.NET文件對象

2009-10-26 11:04:36

VB.NET UDP協(xié)

2009-10-27 14:05:59

VB.NET程序

2009-10-23 14:31:05

VB.NET類定義

2010-01-11 11:37:08

VB.NET操作CSV

2009-10-12 16:39:59

OracleTransVB.NET使用
點贊
收藏

51CTO技術(shù)棧公眾號