登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

miaozk2006

点滴积累与收藏:关于技术,关于生活

 
 
 

日志

 
 

VB中的文件操作  

2012-07-30 21:34:15|  分类: 编程-VB |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
VB 编程中经常需要和文件系统打交道,比如获取硬盘的剩余空间、判断文件夹或文件是否存在等。在VB 推出文件系统对象(File System Object)以前,完成这些功能需要调用 Windows API 函数或者使用一些比较复杂的过程来实现,使编程复杂、可靠性差又容易出错。使用Windows提供的的文件系统对象,一切变得简单多了。以下笔者举出一些编程中比较常用的例子,以函数或过程的形式提供给大家,读者可在编程中直接使用,也可以改进后实现更为强大的功能。
要应用 FSO 对象,须要引用一个名为 Scripting 的类型库,方法是,执行 VB6.0 的菜单项工程/引用,添加引用列表框中的“Microsoft Scripting Runtime”一项。然后我们在对象浏览器中就可以看到 Scripting 类型库下的众多对象及其方法、属性。
1
、判断光驱的盘符:
Function GetCDROM() '/返回光驱的盘符(字母)
Dim Fso As New FileSystemObject '/创建 FSO 对象的一个实例
Dim FsoDrive As Drive, FsoDrives As Drives '/定义驱动器、驱动器集合对象
Set FsoDrives = Fso.Drives
For Each FsoDrive In FsoDrives '/遍历所有可用的驱动器
If FsoDrive.DriveType = CDRom Then '/如果驱动器的类型为 CDrom
GetCDROM = FsoDrive.DriveLetter '/输出其盘符
Else
GetCDROM = ""
End If
Next
Set Fso = Nothing
Set FsoDrive = Nothing
Set FsoDrives = Nothing
End Function

2、判断文件、文件夹是否存在:

'/'返回布尔值:True 存在,False 不存在,filername 文件名
Function FileExist(filename As String)
Dim Fso As New FileSystemObject
If Fso.FileExists(filename) = True Then
FileExist = True
Else
FileExist = False
End If
Set Fso = Nothing
End Function
'/'返回布尔值:True 存在,False 不存在,foldername 文件夹
Function FolderExist(foldername As String)
Dim Fso As New FileSystemObject
If Fso.FolderExists(foldername) = True Then
FolderExist = True
Else
FolderExist = False
End If
Set Fso = Nothing
End Function

3、获取驱动器参数:

''/返回磁盘总空间大小(单位:M),Drive = 盘符 A ,C, D ...
Function AllSpace(Drive As String)
Dim Fso As New FileSystemObject, Drv As Drive
Set Drv = Fso.GetDrive(Drive) '/得到 Drv 对象的实例
If Drv.IsReady Then '/如果该驱动器存在(软驱或光驱里有盘片,硬盘存取正常)
AllSpace = Format$(Drv.TotalSize / (2 ^ 20), "0.00") '/将字节转换为兆
Else
AllSpace = 0
End If
Set Fso = Nothing
Set Drv = Nothing
End Function
'/返回磁盘可用空间大小(单位:M),Drive = 盘符 A ,C, D ...
Function FreeSpace(Drive)
Dim Fso As New FileSystemObject, Drv As Drive
Set Drv = Fso.GetDrive(Drive)
If Drv.IsReady Then
FreeSpace = Format$(Drv.FreeSpace / (2 ^ 20), "0.00")
End If
Set Fso = Nothing
Set Drv = Nothing
End Function
'/获取驱动器文件系统类型,Drive = 盘符 A ,C, D ...
Function FsType(Drive As String)
Dim Fso As New FileSystemObject, Drv As Drive
Set Drv = Fso.GetDrive(Drive)
If Drv.IsReady Then
FsType = Drv.FileSystem
Else
FsType = ""
End If
Set Fso = Nothing
Set Drv = Nothing
End Function

4,获取系统文件夹路径:

'/'返回 Windows 文件夹路径
Function GetWindir()
Dim Fso As New FileSystemObject
GetWindir = Fso.GetSpecialFolder(WindowsFolder)
Set Fso = Nothing
End Function
'/'返回 Windows//System 文件夹路径
Function GetWinSysdir()
Dim Fso As New FileSystemObject
GetWinSysdir = Fso.GetSpecialFolder(SystemFolder)
Set Fso = Nothing
End Function

5,综合运用:一个文件备份通用过程:

''/Filename = 文件名,Drive = 驱动器,Folder = 文件夹(一层)
Sub BackupFile(Filename As String, Drive As String, Folder As String)
Dim Fso As New FileSystemObject '/创建 FSO 对象实例
Dim Dest_path As String, Counter As Long
Counter = 0
Do While Counter < 6 '/如果驱动器没准备好,继续检测。共检测 6 秒
Counter = Counter + 1
Call Waitfor(1) '/间隔 1 秒
If Fso.Drives(Drive).IsReady = True Then
Exit Do
End If
Loop
If Fso.Drives(Drive).IsReady = False Then '/6 秒后目标盘仍未准备就绪,退出
MsgBox " 目标驱动器 " & Drive & " 没有准备好! ", vbCritical
Exit Sub
End If
If Fso.GetDrive(Drive).FreeSpace < Fso.GetFile(Filename).Size Then
MsgBox "目标驱动器空间太小!", vbCritical '/目标驱动器空间不够,退出
Exit Sub
End If
If Right$(Drive, 1) <> ":" Then
Drive = Drive & ":"
End If
If Left$(Folder, 1) <> "//" Then
Folder = "//" & Folder
End If
If Right$(Folder, 1) <> "//" Then
Folder = Folder & "//"
End If
Dest_path = Drive & Folder
If Not Fso.FolderExists(Dest_path) Then '/如果目标文件夹不存在,创建之
Fso.CreateFolder Dest_path
End If
Fso.CopyFile Filename, Dest_path & Fso.GetFileName(Filename), True
'/拷贝,直接覆盖同名文件
MsgBox " 文件备份完毕。", vbOKOnly
Set Fso = Nothing
End Sub
Private Sub Waitfor(Delay As Single) '/延时过程,Delay 单位约为 1 秒
Dim StartTime As Single
StartTime = Timer
Do Until (Timer - StartTime) > Delay
Loop
End Sub

OPEN如下;
VB
读写文件要用到以下语句:
1
Open语句打开文件。
2
、读文件使用Line InputInput #,(以上为文本方式)和Get(以上为二进制方式)。
3
、写文件使用Print #Write(以上为文本方式)和Put(以上为二进制方式)。
4
Close语句关闭文件
5
、二进制方式下移动文件位置使用Seek语句。
所有这些语句在VB的帮助中都有详细说明和例子。
文本文件的示例:

Open "TESTFILE" For Output As #1 '/打开输出文件。
Print #1, "This is a test" '/将文本数据写入文件。
Print #1, '/将空白行写入文件。
Print #1, "Zone 1"; Tab; "Zone 2" '/数据写入两个区(print zones)。
Print #1, "Hello"; " "; "World" '/以空格隔开两个字符串。
Print #1, Spc(5); "5 leading spaces " '/在字符串之前写入五个空格。

Print #1, Tab(10); "Hello" '/将数据写在第十列。

'/赋值 Boolean、Date、Null 及 Error 等。
Dim MyBool, MyDate, MyNull, MyError
MyBool = False
MyDate = #2/12/1969#
MyNull = Null
MyError = CVErr(32767)
'/True、False、Null 及 Error 会根据系统的地区设置自动转换格式。
'/日期将以标准的短式日期的格式显示。
Print #1, MyBool; " is a Boolean value"

Print #1, MyDate; " is a date"
Print #1, MyNull; " is a null value"
Print #1, MyError; " is an error value"
Close #1 '/关闭文件。

读文件示例
使用 Line Input # 语句从顺序文件中读入一行数据,并将该行数据赋予一个变量。本示例假设 TESTFILE 文件内含数行文本数据。

Dim TextLine

Open "TESTFILE" For Input As #1 '/打开文件。
Do While Not EOF(1) '/循环至文件尾。
Line Input #1, TextLine '/读入一行数据并将其赋予某变量。
Debug.Print TextLine '/在调试窗口中显示数据。
Loop
Close #1 '/关闭文件。

摘自:网络整理



VB相关



VB6 中善用ByRef 提升速度

[vb] Set 语句

VB_Format自定义格式

VB如读取内存地址

vb FindwindowEx的用法实例

进制转化进10进制数

收藏:如何获取当前已经打开的IE对象(VB6代码)

DXInput中键码的转换(VB6.0代码)

如何在VB6.0里动态使用具有事件的对象

[vb]格式输出Format函数

读取和写入WindowsINI文件

简述UTF8编码原理及其文本文件的读写技术【转】

VB中的文件操作

VB中的文件操作文档

vb 中拷贝文件

VB反跟踪技术点滴

VB共享软件防破解设计技术初探(二)

VB共享软件防破解设计技术初探(三)

VB共享软件防破解设计技术初探(一)

RTF文件格式【转】

VB压缩技术

[vb]FSO对象模型在VB中的应用

VB 窗体实现文件拖拽获取路径方法

VB:注册表的读写

vb中空操作(等待)的指令、延时方法

VB让控件可以当标题栏拖动

FSO对象新建、打开、保存文件

获取网关IPMAC VB源码

VB文件关联

vb获得本地和远程的MAC地址(网卡地址)

VBShellExeCute的应用

VB打开网址方法大全

vb简单控制音量大小及静音的方法

拖动无边框窗体(VB6代码)

VB使用FileSystemObject对象写文件

VB 从注册表中删除项及其某个值

vb 字符串转为数字和判断字符串是否是数字字符串【转】

vb按热键启动应用程序

VB的坐标系统综述

VB利用API函数来处理文件

关于VBShellShellExecute的总结与记录

[vb]On Error GoTo 0On Error resume区别

[vb]On Error 语句

记录一下:在菜单上添加自绘图形的例子(VB6代码)

vbfindwindow的疑惑

[vb]FindWindow使用方法

常用文件类[转,无法运行通,待调试]

[vb]url utf-8编码

VB中的Unicode Ansi 格式

VB中的format格式化函数

VB中字符串匹配的多种方式

VB抓图

vb目录文件操作的三种方法-2

vb目录文件操作的三种方法-1

vb使用open方法读写文件

VBMD5加密模块

VB 超简单的屏幕截图代码

vb以类名或窗口标题查找句柄并关闭

VB将配置保存到EXE本身(生成EXE木马程序)

VB 调用腾讯截图控件CameraDLL.dll

VB6.0中怎么实现escapeunescape

vb求任意两线交点

VB中调用Windows API的注意事项[VB知识库]

VB 一个获得自己外网IP 地址的程序代码

VB程序中实现IP地址子网掩码网关DNS的更改[]

VB 中应用FSO 对象模型介绍(摘自网络)

[] VbFSO 对象的介绍

VB 画坐标轴

VB 二进制文件的操作

[VB]BMPJPG

VBKeyCode常数用法

vb实时曲线的绘制和保存


更多精彩>>>
  评论这张
 
阅读(1046)| 评论(0)

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018