工作總結VB.NET文件操作三個方法
作者:佚名
這里介紹了VB.NET文件操作的三個方法:判斷光驅的盤符、判斷文件、文件夾是否存在、獲取驅動器參數。文章用代碼的形式為大家講解。
在工作中總結了一些關于VB.NET文件操作。現在給大家舉出一些編程中比較常用的例子,以函數或過程的形式提供給大家,讀者可在編程中直接使用,也可以改進后實現更為強大的功能。
1、VB.NET文件操作之判斷光驅的盤符:
- FunctionGetCDROM()\'返回光驅的盤符(字母)
- DimFsoAsNewFileSystemObject\'創建FSO對象的一個實例
- DimFsoDriveAsDrive,FsoDrivesAsDrives\'定義驅動器、驅動器集合對象
- SetFsoDrives=Fso.Drives
- ForEachFsoDriveInFsoDrives\'遍歷所有可用的驅動器
- IfFsoDrive.DriveType=CDRomThen\'如果驅動器的類型為CDrom
- GetCDROM=FsoDrive.DriveLetter\'輸出其盤符
- Else
- GetCDROM=""
- EndIf
- Next
- SetFso=Nothing
- SetFsoDrive=Nothing
- SetFsoDrives=Nothing
- EndFunction
2、VB.NET文件操作之判斷文件、文件夾是否存在:
- \'返回布爾值:True存在,False不存在,filername文件名
- FunctionFileExist(filenameAsString)
- DimFsoAsNewFileSystemObject
- IfFso.FileExists(filename)=TrueThen
- FileExist=True
- Else
- FileExist=False
- EndIf
- SetFso=Nothing
- EndFunction
- \'返回布爾值:True存在,False不存在,foldername文件夾
- FunctionFolderExist(foldernameAsString)
- DimFsoAsNewFileSystemObject
- IfFso.FolderExists(foldername)=TrueThen
- FolderExist=True
- Else
- FolderExist=False
- EndIf
- SetFso=Nothing
- EndFunction
3、VB.NET文件操作之獲取驅動器參數:
- \'返回磁盤總空間大小(單位:M),Drive=盤符A,C,D...
- FunctionAllSpace(DriveAsString)
- DimFsoAsNewFileSystemObject,DrvAsDrive
- SetDrv=Fso.GetDrive(Drive)\'得到Drv對象的實例
- IfDrv.IsReadyThen\'如果該驅動器存在(軟驅或光驅里有盤片,硬盤存取正常)
- AllSpace=Format(Drv.TotalSize/(2^20),"0.00")\'將字節轉換為兆
- Else
- AllSpace=0
- EndIf
- SetFso=Nothing
- SetDrv=Nothing
- EndFunction
- \'返回磁盤可用空間大小(單位:M),Drive=盤符A,C,D...
- FunctionFreeSpace(drive)
- DimFsoAsNewFileSystemObject,drvAsdrive
- Setdrv=Fso.GetDrive(drive)
- Ifdrv.IsReadyThen
- FreeSpace=Format(drv.FreeSpace/(2^20),"0.00")
- EndIf
- SetFso=Nothing
- SetDrv=Nothing
- EndFunction
- \'獲取驅動器文件系統類型,Drive=盤符A,C,D...
- FunctionFsType(DriveAsString)
- DimFsoAsNewFileSystemObject,DrvAsDrive
- SetDrv=Fso.GetDrive(Drive)
- IfDrv.IsReadyThen
- FsType=Drv.FileSystem
- Else
- FsType=""
- EndIf
- SetFso=Nothing
- SetDrv=Nothing
- EndFunction
【編輯推薦】
責任編輯:田樹
來源:
博客