成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

代碼演示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-10-29 13:46:14

VB.NET DES加

2009-11-03 11:06:40

VB.NET事件

2009-10-28 15:18:46

VB.NET網絡應用

2009-10-26 09:50:20

VB.NET Star

2009-10-26 14:50:18

VB.NET遍歷注冊表

2009-10-26 10:30:57

VB.NET處理FTP

2010-01-21 16:17:32

VB.NET文件對象

2009-10-09 15:59:41

VB.NET對象

2009-10-14 13:21:46

VB.NET Acco

2010-01-20 13:42:10

VB.NET訪問INIGetPrivateP

2009-10-27 16:36:46

VB.NET文件流

2009-10-23 14:31:05

VB.NET類定義

2009-10-27 14:05:59

VB.NET程序

2009-10-26 11:04:36

VB.NET UDP協

2010-01-15 10:05:35

VB.NET文件對象

2009-10-30 11:20:54

VB.NET Proc

2010-01-12 18:05:38

VB.NET對象
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 电影91久久久 | 免费在线观看一区二区 | 国产一区在线免费观看视频 | 国产精品1区 | 亚洲国产精品一区二区久久 | 国产精品自拍视频 | 精品国产91乱码一区二区三区 | 国产精品久久久久久久7电影 | 亚洲国产精品久久久久 | 欧美激情亚洲天堂 | 美女露尿口视频 | 精品国产视频 | 自拍偷拍av | 围产精品久久久久久久 | 亚洲精品黑人 | 91亚洲国产 | 国产一区二区三区在线视频 | 国产一区在线免费观看 | 久久国产精99精产国高潮 | 一级看片| 色吊丝在线 | 欧美在线一区二区三区四区 | 精品视频免费 | 狠狠草视频| 精品乱子伦一区二区三区 | 亚洲精品久久久久中文字幕欢迎你 | 黄色大片毛片 | 国产一区欧美一区 | 日批免费看 | 久久久亚洲 | 国产高清在线 | 日本色婷婷 | 欧美在线视频一区 | 亚洲天堂男人的天堂 | 天天躁人人躁人人躁狂躁 | 欧美日韩亚洲国产 | 国产一级特黄视频 | 欧美精品中文字幕久久二区 | 亚洲精品久久久久久久久久久 | 成人妇女免费播放久久久 | 国产精品区二区三区日本 |