OpenFile用法,想在98下,禁用所有类型的文件,用这个函数打开文件是不是就行了?有例子吗?
解决方案 »
- 动态数组Redim 传参数的问题
- 100分求超级简单的问题答案,在线等。关于数据库查询的。
- 用API怎样得到计算机的可用的串口
- 如何判断rs(1)的值是布尔类型或者是空
- vb初学者,再次请教
- 求助。
- ****怎么样吧一个顺序访问方式打开的文件清空?
- 各位大侠,请问在sql server2000中执行"SELECT * INTO [;DATABASE=c:\db2.mdb].BK_T1 FROM T1"语句会出错?
- 这个问题难道真的这么难,!!!!!!!!!!如果分不够可以再加。
- 高手在吗,如何用Crystalreport访问设有密码的Access数据库。
- 一个弱弱的问题,一定给分!
- 关于VB中的几个Access编程问题
Const OF_CREATE = &H1000
Const OF_READ = &H0
Const OF_WRITE = &H1
Private Type OFSTRUCT
cBytes As Byte
fFixedDisk As Byte
nErrCode As Integer
Reserved1 As Integer
Reserved2 As Integer
szPathName(OFS_MAXPATHNAME) As Byte
End Type
Private Declare Function CopyLZFile Lib "lz32" (ByVal n1 As Long, ByVal n2 As Long) As Long
Private Declare Function OpenFile Lib "kernel32" (ByVal lpFileName As String, lpReOpenBuff As OFSTRUCT, ByVal wStyle As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Sub Form_Load()
'KPD-Team 1998
'URL: http://www.allapi.net/
'E-Mail: [email protected]
Dim sFile As String, hDecomp As Long, hResult As Long
Dim OF As OFSTRUCT, sSaveTo As String
'Ask for a file to decompress
sFile = InputBox("Please, enter a file to decompress.")
'Ask for a file to decompress it to
sSaveTo = InputBox("Please, enter a filename to decompress it to.")
'Open the two files
hDecomp = OpenFile(sFile, OF, OF_READ)
hResult = OpenFile(sSaveTo, OF, OF_WRITE Or OF_CREATE)
'decompress the file
CopyLZFile hDecomp, hResult
'Close the two files
CloseHandle hDecomp
CloseHandle hResult
Unload Me
End Sub
有个使用的例子供你参考,模块内容:
Public Const OFS_MAXPATHNAME = 128
Public Const OF_READ = &H0
Public Type OFSTRUCT
cBytes As Byte
fFixedDisk As Byte
nErrCode As Integer
Reserved1 As Integer
Reserved2 As Integer
szPathName(OFS_MAXPATHNAME) As Byte
End TypePublic Type SYSTEMTIME
wYear As Integer
wMonth As Integer
wDayOfWeek As Integer
wDay As Integer
wHour As Integer
wMinute As Integer
wSecond As Integer
wMilliseconds As Integer
End TypePublic Type FileTime
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Public Type BY_HANDLE_FILE_INFORMATION
dwFileAttributes As Long
ftCreationTime As FileTime
ftLastAccessTime As FileTime
ftLastWriteTime As FileTime
dwVolumeSerialNumber As Long
nFileSizeHigh As Long
nFileSizeLow As Long
nNumberOfLinks As Long
nFileIndexHigh As Long
nFileIndexLow As Long
End TypePublic Type TIME_ZONE_INFORMATION
bias As Long
StandardName(32) As Integer
StandardDate As SYSTEMTIME
StandardBias As Long
DaylightName(32) As Integer
DaylightDate As SYSTEMTIME
DaylightBias As Long
End Type
Public Declare Function GetTimeZoneInformation Lib "kernel32" (lpTimeZoneInformation As TIME_ZONE_INFORMATION) As Long
Public Declare Function OpenFile Lib "kernel32" (ByVal lpFileName As String, lpReOpenBuff As OFSTRUCT, ByVal wStyle As Long) As Long
Public Declare Function GetFileInformationByHandle Lib "kernel32" (ByVal hFile As Long, lpFileInformation As BY_HANDLE_FILE_INFORMATION) As Long
Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Public Declare Function FileTimeToSystemTime Lib "kernel32" (lpFileTime As FileTime, lpSystemTime As SYSTEMTIME) As Long
Public Const OF_READWRITE = &H2
Public Declare Function SetFileTime Lib "kernel32" (ByVal hFile As Long, lpCreationTime As Any, lpLastAccessTime As Any, lpLastWriteTime As Any) As Long
Public Declare Function SystemTimeToFileTime Lib "kernel32" (lpSystemTime As SYSTEMTIME, lpFileTime As FileTime) As Long
Public Declare Sub GetSystemTime Lib "kernel32" (lpSystemTime As SYSTEMTIME)
Public Declare Sub GetLocalTime Lib "kernel32" (lpSystemTime As SYSTEMTIME)
Private hFile As Long
Private Sub Command1_Click()
Dim FileHandle As Long
Dim FileInfo As BY_HANDLE_FILE_INFORMATION
Dim lpReOpenBuff As OFSTRUCT, ft As SYSTEMTIME
Dim tZone As TIME_ZONE_INFORMATION
Dim dtCreate As Date ' 建立时间。
Dim dtAccess As Date ' 存取日期。
Dim dtWrite As Date ' 修改时间。
Dim bias As Long
' 先取得文件的Handle。
FileHandle = OpenFile(Text2.Text, lpReOpenBuff, OF_READ)
' 利用文件的Handle读取文件信息。
Call GetFileInformationByHandle(FileHandle, FileInfo)
Call CloseHandle(FileHandle)
' 读取时间信息, 因为上一步骤的文件时间是格林威治时间。
Call GetTimeZoneInformation(tZone)
bias = tZone.bias ' 时间差, 以分为单位。
Call FileTimeToSystemTime(FileInfo.ftCreationTime, ft) ' 转换时间结构。
dtCreate = DateSerial(ft.wYear, ft.wMonth, ft.wDay) + TimeSerial(ft.wHour, ft.wMinute - bias, ft.wSecond)
Call FileTimeToSystemTime(FileInfo.ftLastAccessTime, ft)
dtAccess = DateSerial(ft.wYear, ft.wMonth, ft.wDay) + TimeSerial(ft.wHour, ft.wMinute - bias, ft.wSecond)
Call FileTimeToSystemTime(FileInfo.ftLastWriteTime, ft)
dtWrite = DateSerial(ft.wYear, ft.wMonth, ft.wDay) + TimeSerial(ft.wHour, ft.wMinute - bias, ft.wSecond)
MsgBox "建立时间:" + CStr(dtCreate)
MsgBox "修改时间:" + CStr(dtWrite)
MsgBox "存取时间:" + CStr(dtAccess)
End Sub
Private Sub Command2_Click()
Dim lpct As FileTime, lplac As FileTime, lplwr As FileTime
Dim ofs As OFSTRUCT
Dim tZone As TIME_ZONE_INFORMATION
Dim ft As SYSTEMTIME
Dim dtdate As Date
Dim bias As Long
hFile = OpenFile(Text2.Text, ofs, OF_READWRITE)
Call GetTimeZoneInformation(tZone)
bias = tZone.bias ' 时间差, 以分为单位。
'计算出格林威治时间。
dtdate = CDate(Text1.Text) + TimeSerial(0, bias, 0)
ft.wYear = Year(dtdate)
ft.wMonth = Month(dtdate)
ft.wDay = Day(dtdate)
ft.wHour = Hour(dtdate)
ft.wMinute = Minute(dtdate)
ft.wSecond = Second(dtdate)
ft.wDayOfWeek = Weekday(dtdate)
ft.wMilliseconds = 0
Call SystemTimeToFileTime(ft, lplwr)
'更动hFile的时间,第2个参数改Create DateTime。
'第3个参数改Last Access DateTime。
'第4个参数改Last Modify DateTime。
Call SetFileTime(hFile, ByVal 0, ByVal 0, lplwr)
Call CloseHandle(hFile)
End Sub
Private Sub Form_Load()
Text1.Text = "1998/06/03 5:50:10 AM"
Text2.Text = "c:\autoexec.bat"
End Sub
Declare Function OpenFile Lib "kernel32" Alias "OpenFile" (ByVal lpFileName As String, lpReOpenBuff As OFSTRUCT, ByVal wStyle As Long) As Long
说明
这个函数能执行大量不同的文件操作。和这个函数相比,请优先考虑win32的CreateFile函数(它能打开命名管道和控制Unicode文件名,同时不受128个字符的路径名称的限制)
返回值
Long,如执行成功,返回文件句柄。注意文件句柄可能是无效的;例如,假设指定了OF_EXIST标志,文件在函数返回前会关闭,但它打开时的句柄却永远不会返回。如果出错,函数会返回HFILE_ERROR;此时,由lpReOpenBuff指定的OFSTRUCT结构的nErrCode会设置成发生的错误。表OpenFile-2(OFSTRUCT出错代码)对这些错误进行了总结。会设置GetLastError
参数表
参数 类型及说明
lpFileName String,欲打开文件的名字
lpReOpenBuff OFSTRUCT,该结构填充的数据包括与文件和操作结果有关的信息
wStyle Long,参考表OpenFile-1(OpenFile函数的标志常数表)总结的标志常数的组合,它决定了要采取的操作方式 表OpenFile-1(OpenFile函数的标志常数表)
wStyle常数 说明
OF_CREATE 创建指定的文件。如已经存在,则将其缩减为零长度
OF_DELETE 删除指定的文件
OF_EXIST 通过尝试打开文件的做法,判断一个文件是否存在。如文件存在,则将其关闭。此时,函数会返回文件打开时使用的句柄,但这个句柄是无效的。如指定的文件不存在,则返回一个负数
OF_PARSE 填写lpReOpenBuff结构的内容,但不执行其他任何操作
OF_PROMPT 如文件不存在,则显示一个消息框,在其中列出重试和取消按钮
OF_READ 以只读方式打开文件
OF_READWRITE 以可读、可写的方式打开文件
OF_REOPEN 打开lpReOpenBuff结构内指定的文件,而不是用lpFileName参数
OF_SEARCH 强迫windows查找文件——即使指定了特定的路径
OF_SHARE_COMPAT 文件可由多个应用程序打开多次
OF_SHARE_DENY_NONE 可打开文件,以便由其他程序读写
OF_SHARE_DENY_READ 禁止其他程序读写文件内容
OF_SHARE_DENY_WRITE 其他程序可以读文件,但不能写文件
OF_SHARE_EXCLUSIVE 其他任何一个程序都不能再打开这个文件
OF_WRITE 文件以只写模式打开
表OpenFile-2(OFSTRUCT出错代码)
十六进制值 说明 十六进制值 说明
1 函数无效 2 文件未找到
3 路径未找到 4 无可用文件句柄
5 拒绝访问 6 句柄无效
7 DOS内存冲突 8 无足够内存完成操作
9 无效块 A 非法环境
B 无效格式 C 无效访问
D 无效数据
F 无效驱动器 10 当前目录无效
11 设备有异 12 没有更多的文件
13 写保护错 14 非法单位
15 驱动器未准备好 16 无效命令
17 CRC校验错 18 无效长度
19 搜索错误 1A 磁盘不兼容MS-DOS
1B 扇区未找到 1C 缺纸
1D 写错误 1E 读错误
1F 驱动器常规错误 20 共享违例
21 文件锁定违例 22 不正确的磁盘
23 无可用的文件控制块 24 共享缓冲区溢出
32 不支持的设备 33 远程设备不可用
34 重名错误 35 网络路径错误
36 网络忙 37 非法设备
38 命令太多 39 网卡硬件错误
3A 网络响应错误 3B 其他网络错误
3C 远程适配器错误 3D 打印队列满
3E 后台打印缓冲区满 3F 打印取消
40 删除的网络名 41 拒绝网络访问
42 无效设备类型 43 无效网络名
44 名字太多 45 会话太多
46 共享暂停 47 请求未接受
48 重定向暂停 50 文件退出
51 文件控制块重复 52 不能创建
53 中断24错误 54 缺少结构
55 已经分配 56 密码无效
57 参数无效 58 网络写错误
Const OFS_MAXPATHNAME = 128
Const OF_CREATE = &H1000
Const OF_READ = &H0
Const OF_WRITE = &H1
Private Type OFSTRUCT
cBytes As Byte
fFixedDisk As Byte
nErrCode As Integer
Reserved1 As Integer
Reserved2 As Integer
szPathName(OFS_MAXPATHNAME) As Byte
End Type
Private Declare Function CopyLZFile Lib "lz32" (ByVal n1 As Long, ByVal n2 As Long) As Long
Private Declare Function OpenFile Lib "kernel32" (ByVal lpFileName As String, lpReOpenBuff As OFSTRUCT, ByVal wStyle As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Sub Form_Load()
'KPD-Team 1998
'URL: http://www.allapi.net/
'E-Mail: [email protected]
Dim sFile As String, hDecomp As Long, hResult As Long
Dim OF As OFSTRUCT, sSaveTo As String
'Ask for a file to decompress
sFile = InputBox("Please, enter a file to decompress.")
'Ask for a file to decompress it to
sSaveTo = InputBox("Please, enter a filename to decompress it to.")
'Open the two files
hDecomp = OpenFile(sFile, OF, OF_READ)
hResult = OpenFile(sSaveTo, OF, OF_WRITE Or OF_CREATE)
'decompress the file
CopyLZFile hDecomp, hResult
'Close the two files
CloseHandle hDecomp
CloseHandle hResult
Unload Me
End Sub