问题描述:
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,如果不为零,则指定一个文件句柄。新文件将从这个文件中复制扩展属性
string activeDir = @"C:\myDir"string newPath = System.IO.Path.Combine(activeDir, "mySubDirOne")
2、目录的创建。
创建目录时如果目录已存在,则不会重新创建目录,且不会报错。创建目录时会自动创建路径中各级不存在的目录。
(1)通过Directory类的CreateDirectory方法创建。
string activeDir = @"C:\myDir"
string newPath = System.IO.Path.Combine(activeDir, "mySubDirOne")
System.IO.Directory.CreateDirectory(newPath)
(1)通过DirectoryInfo的对象创建。
System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(@"C:\myDirTwo\mySubDirThree")
di.Create()
3、文件的创建。
通过Create方法创建文件,会覆盖同名的现有文件。创建文件时,该文件所在路径的目录必须存在,否则报错。
(1)通过File类的Create方法创建。
string activeDir = @"C:\myDir"
string newPath = System.IO.Path.Combine(activeDir, "mySubDirOne")
System.IO.Directory.CreateDirectory(newPath)
//创建一个空白文件
string fileNameOne = DateTime.Now.ToString("yyyyMMddHHmmssffff")
+ ".txt"
string filePathOne = System.IO.Path.Combine(newPath, fileNameOne)
System.IO.File.Create(filePathOne)
(2)通过FileInfo对象创建。
//通过Combine合并目录
//然后创建目录
string activeDir = @"C:\myDir"
string newPath = System.IO.Path.Combine(activeDir, "mySubDirOne")
System.IO.Directory.CreateDirectory(newPath)
//创建一个空白文件
string fileNameOne = DateTime.Now.ToString("yyyyMMddHHmmssffff")
+ ".txt"
string filePathOne = System.IO.Path.Combine(newPath, fileNameOne)
System.IO.FileInfo fi = new System.IO.FileInfo(filePathOne)
fi.Create()
复制目录文件
//复制单个文件到指定目录
string fileName = "test.txt"
string sourcePath = @"C:\testDir\subTestDir"
string targetPath = @"C:\testDir\subTestDirTwo"
string sourceFile = System.IO.Path.Combine(sourcePath, fileName)
string destFile = System.IO.Path.Combine(targetPath, fileName)
if (!System.IO.Directory.Exists(targetPath))
System.IO.Directory.CreateDirectory(targetPath)
//如果已存在,参数为false时将报错,参数为true重写该文件
//当copy方法为两个参数时,默认重写为false。
System.IO.File.Copy(sourceFile, destFile, true)
//以下为复制一个目录下所有文件到指定目录
//如果复制有子目录的目录的所有文件,可以用递归或堆栈算法实现
if (System.IO.Directory.Exists(sourcePath))
{
string[] files = System.IO.Directory.GetFiles(sourcePath)
foreach (string s in files)
{
//仅返回路径字符串的文件名及后缀
fileName = System.IO.Path.GetFileName(s)
destFile = System.IO.Path.Combine(targetPath, fileName)
System.IO.File.Copy(s, destFile, true)
}
}
}
移动目录和文件
/*移动文件*/
string sourceFile = @"C:\testDir\subTestDir\test.txt"
string destFile = @"C:\testDir\subTestDirTwo\test.txt"
//当目标文件存在时,抛出异常
System.IO.File.Move(sourceFile, destFile)
/*移动目录*/
//移动目录将移动改目录的子目录和文件
System.IO.Directory.Move(@"C:\testDir\subTestDirTwo\", @"C:\testDir\subTestDir")
删除目录和文件
1、删除目录
删除目录,如果该目录不存在,会抛出异常。可以通过File类的Delete方法删除目录,也可以通过FileInfo对象方法删除目录。
(1)通过 File类的Delete方法删除目录
//删除可写空目录
//如果不为空抛出目录不为空异常
try
{
System.IO.Directory.Delete(@"C:\testDir\subTestDir")
}
catch (System.IO.IOException e)
{
Console.WriteLine(e.Message)
}
//第二参数为false时,只能删除空目录,否则抛出不为空异常
//第二参数为true时,删除目录,包括子目录和文件
try
{
System.IO.Directory.Delete(@"C:\testDir\subTestDir", true)
}
catch(System.IO.IOException e)
{
Console.WriteLine(e.Message)
}
(2)通过FileInfo对象方法删除目录
System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(@"C:\testDir\subTestDirTwo")
try
{
//无参数删除空目录
//当参数为false,可删除空目录为true,删除目录,包括子目录和文件
di.Delete(true)
}
catch (System.IO.IOException e)
{
Console.WriteLine(e.Message)
}
2、删除文件
删除文件时如果指定文件的目录存在,而文件不存在,则不会抛出异常,如果指定文件的目录不存在,则会抛出异常。
(1)通过File类Delete方法删除文件
try
{
System.IO.File.Delete(@"C:\testDir\subTestDir\test.txt")
}
catch(System.IO.IOException e)
{
Console.WriteLine(e.Message)
}
(2)通过FileInfo对象Delete方法删除文件
System.IO.FileInfo fi = new System.IO.FileInfo(@"C:\testDir\subTestDir\test1.txt")
try
{
fi.Delete()
}
catch(System.IO.IOException e)
{
Console.WriteLine(e.Message)
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)