在Text1里有一个绝对路径,判断是否存在这个文件夹。
存在则d出对碰圆搏话框:腔稿文件已存在;不存在则依照路径新建文件夹并d出对话框:创笑祥建完毕!
1、vb6新建文本文件有很多敏圆野种办法。
2、以下示例使用open语句的output方式桥喊腔段创建d:盘根目录下的TESTFILE.TXT文本文件。
Private Sub Command1_Click()Open "d:\TESTFILE.TXT" For Output As #1
Print #1, "Hello world."
Close #1
End Sub
分类: 电脑/网络 >>程序设计 >>其他编程语言问题描述:
vb60编程中,如何用API函数中的createfile()创建一个文件?能否给出具体的代码?我要创戚敏建的是一个text文件,因为这个函数塌缓的参数太多了,看不懂?
解析:
实例
'This project needs a Common Dialog box, named CDBox.
' (To add the Common Dialog Box to your tools menu, go to Project->Components (or press CTRL-T)
' and select Microsoft Common Dialog control)
Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Private 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 Type
Private Const GENERIC_WRITE = &H***********
Private Const OPEN_EXISTING = 3
Private Const FILE_SHARE_READ = &H1
Private Const FILE_SHARE_WRITE = &H2
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 Declare Function SetFileTime Lib "kernel32" (ByVal hFile As Long, lpCreationTime As FILETIME, lpLastAccessTime As FILETIME, lpLastWriteTime As FILETIME) As Long
Private Declare Function SystemTimeToFileTime Lib "kernel32" (lpSystemTime As SYSTEMTIME, lpFileTime As FILETIME) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function LocalFileTimeToFileTime Lib "kernel32" (lpLocalFileTime As FILETIME, lpFileTime As FILETIME) As Long
Private Sub Form_Load()
Dim m_Date As Date, lngHandle As Long
Dim udtFileTime As FILETIME
Dim udtLocalTime As FILETIME
Dim udtSystemTime As SYSTEMTIME
m_Date = Format(Now, "DD-MM-YY")
'Set the dialog's title
CDBox.DialogTitle = "Choose a file ..."
'Set the dialog's filter
CDBox.Filter = "All Files (*.*)|*.*"
'Show the 'Open File'-dialog
CDBox.ShowOpen
udtSystemTime.wYear = Year(m_Date)
udtSystemTime.wMonth = Month(m_Date)
udtSystemTime.wDay = Day(m_Date)
udtSystemTime.wDayOfWeek = WeekDay(m_Date) - 1
udtSystemTime.wHour = Hour(m_Date)
udtSystemTime.wSecond = Second(m_Date)
udtSystemTime.wMilliseconds = 0
' convert system time to local time
SystemTimeToFileTime udtSystemTime, udtLocalTime
' convert local time to GMT
LocalFileTimeToFileTime udtLocalTime, udtFileTime
' open the file to get the filehandle
lngHandle = CreateFile(CDBox.Filename, GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, ByVal 0&, OPEN_EXISTING, 0, 0)
' change date/time property of the file
SetFileTime lngHandle, udtFileTime, udtFileTime, udtFileTime
' close the handle
CloseHandle lngHandle
MsgBox "The date of the file '" + CDBox.Filename + "' has been changed to" + Str$(m_Date), vbInformation + vbOKOnly, App.Title
End Sub
附:
【VB声明】
Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, lpSecurityAttributes As SECURITY_ATTRIBUTES, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
【别名】
CreateFileA
【说明】
这是一个全功能的例程,可打开和创建文件、管道、邮槽、通信服务、设备以及控制台
【返回值】
Long,如执行成功,则返回文件句柄。INVALID_HANDLE_VALUE表示出错,会设置GetLastError。即使函数成功,但若文件存在,且指定了CREATE_ALWAYS
或 OPEN_ALWAYS,GetLastError也会设为ERROR_ALREADY_EXISTS
【备注】
打开一个通信端口时(如COM1),无论如何都要设置成
OPEN_EXISTING
这个函数代替了lOpen 和 lCreate函数,应该是我们的首选
【参数表】
lpFileName ----- String,要打开的文件的名字
dwDesiredAccess - Long,如果为 GENERIC_READ
表示允许对设备进行读访问;如果为 GENERIC_WRITE
表示允许对设备进行写访问(可组合使用);如果为零,表示只允许获取与一个设备有关的信息
dwShareMode ---- Long,零表示不共享; FILE_SHARE_READ 和/或
FILE_SHARE_WRITE 表示允许对文件进行共享访问
lpSecurityAttributes - SECURITY_ATTRIBUTES,指向一个SECURITY_ATTRIBUTES结构的指针,定义了文件的安全特性(如果 *** 作系统支持的话)
dwCreationDisposition - Long,下述常数之一:
CREATE_NEW
创建文件;如文件存在则会出错
CREATE_ALWAYS
创建文件,会改写前一个文件
OPEN_EXISTING
文件必须已经存在。由设备提出要求
OPEN_ALWAYS
如文件不存在则创建它
TRUNCATE_EXISTING
讲现有文件缩短为零长度
dwFlagsAndAttributes - Long,一个或多个下述常数
FILE_ATTRIBUTE_ARCHIVE
标记归档属性
FILE_ATTRIBUTE_COMPRESSED
将文件标记为已压缩,或者标记为文件在目录中的默认压缩方式
FILE_ATTRIBUTE_NORMAL
默认属性
FILE_ATTRIBUTE_HIDDEN
隐藏文件或目录
FILE_ATTRIBUTE_READONLY
文件为只读
FILE_ATTRIBUTE_SYSTEM
文件为系统文件
FILE_FLAG_WRITE_THROUGH
*** 作系统不得推迟对文件的写 *** 作
FILE_FLAG_OVERLAPPED
允许对文件进行重叠 *** 作
FILE_FLAG_NO_BUFFERING
禁止对文件进行缓冲处理。文件只能写入磁盘卷的扇区块
FILE_FLAG_RANDOM_ACCESS
针对随机访问对文件缓冲进行优化
FILE_FLAG_SEQUENTIAL_SCAN
针对连续访问对文件缓冲进行优化
FILE_FLAG_DELETE_ON_CLOSE
关闭了上一次打开的句柄后,将文件删除。特别适合临时文件
也可在Windows NT下组合使用下述常数标记:
SECURITY_ANONYMOUS, SECURITY_IDENTIFICATION,
SECURITY_IMPERSONATION, SECURITY_DELEGATION, SECURITY_CONTEXT_TRACKING,
SECURITY_EFFECTIVE_ONLY
hTemplateFile -- Long,如果不为零,则指定一个文件句柄。新文件将从这个文件中复制扩展属性
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)