vb60編程中,為什么我用CreateFile函數(shù)無(wú)法創(chuàng)建文件? 代碼如下: Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal lpSecurityAttributes As Long, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long Private Const GENERIC_WRITE = &H40000000 Private Const OPEN_EXISTING = 3 Private Const FILE_SHARE_READ = &H1 Private Const FILE_SHARE_WRITE = &H2 dim lngHandle As Long lngHandle = CreateFile("C:\email126.txt", GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, ByVal 0&, OPEN_EXISTING, 0, 0) '創(chuàng)建文件 經(jīng)運(yùn)行測(cè)試 lngHandle的值為-1,但查找文件時(shí)卻找不到,文件沒(méi)有創(chuàng)建!
2 回答

精慕HU
TA貢獻(xiàn)1845條經(jīng)驗(yàn) 獲得超8個(gè)贊
因?yàn)槟阌昧?OPEN_EXISTING,前提必須要有c:\email126.txt這個(gè)文件才能成功,如果你第一次運(yùn)行,肯定是-1,如果只是創(chuàng)建一個(gè)新文件,要用 CREATE_ALWAYS 或 OPEN_ALWAYS
如:
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Const CREATE_ALWAYS As Long = 2
Private Const OPEN_ALWAYS As Long = 4
lngHandle = CreateFile("C:\email126.txt", GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, ByVal 0&, CREATE_ALWAYS, 0, 0) '創(chuàng)建文件
Call CloseHandle(lngHandle) '關(guān)閉lngHandle
- 2 回答
- 0 關(guān)注
- 1167 瀏覽
添加回答
舉報(bào)
0/150
提交
取消