一般文件的清空(没试过系统锁定文件): Open XXX For Output As #1 '用Output 打开会自动清空 Close 1
Open file For Output As #1 close #1 如果文件是只读的,就没有用了,还需要先改变文件的只读属性!
用api函数SetEndOfFile的写法: Option Explicit Const FILE_BEGIN = 0 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 TypePrivate 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 Declare Function SetEndOfFile Lib "kernel32" (ByVal hFile As Long) As Long Private Declare Function SetFilePointer Lib "kernel32" (ByVal hFile As Long, ByVal lDistanceToMove As Long, lpDistanceToMoveHigh As Long, ByVal dwMoveMethod As Long) As LongPrivate Sub Command1_Click() Dim mfile As String mfile = "e:\db1.mdb" '改成你的文件路径 clearFile mfile End SubPrivate Sub clearFile(ByVal mfile As String) Dim OF As OFSTRUCT Dim hFile As Long hFile = OpenFile(mfile, OF, OF_READ Or OF_WRITE) SetFilePointer hFile, 0, 0, FILE_BEGIN SetEndOfFile hFile CloseHandle hFile End Sub
print#1,""
close #1
put#1, s, ""
close #1
Open XXX For Output As #1 '用Output 打开会自动清空
Close 1
close #1
如果文件是只读的,就没有用了,还需要先改变文件的只读属性!
Option Explicit
Const FILE_BEGIN = 0
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 TypePrivate 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 Declare Function SetEndOfFile Lib "kernel32" (ByVal hFile As Long) As Long
Private Declare Function SetFilePointer Lib "kernel32" (ByVal hFile As Long, ByVal lDistanceToMove As Long, lpDistanceToMoveHigh As Long, ByVal dwMoveMethod As Long) As LongPrivate Sub Command1_Click()
Dim mfile As String
mfile = "e:\db1.mdb" '改成你的文件路径
clearFile mfile
End SubPrivate Sub clearFile(ByVal mfile As String)
Dim OF As OFSTRUCT
Dim hFile As Long
hFile = OpenFile(mfile, OF, OF_READ Or OF_WRITE)
SetFilePointer hFile, 0, 0, FILE_BEGIN
SetEndOfFile hFile
CloseHandle hFile
End Sub