Public Function TempName(sPath As String, ByVal sPrefix As String, WriteIn As String) As String Dim lUnique As Long
Dim sTempFileName As String
Dim slen As Long
Dim fh As Long
Dim wd As Boolean
Dim wlen As Long If IsEmpty(sPath) Then sPath = ".\WebNetEx\temp\"
If IsEmpty(sPrefix) Then sPrefix = "SPIDER"
lUnique = 0 'windows随机给名 sTempFileName = Space$(100)
GetTempFileName sPath, sPrefix, lUnique, sTempFileName
sTempFileName = Mid$(sTempFileName, 1, InStr(sTempFileName, Chr$(0)) - 1) '去掉多余空格 slen = LenB(WriteIn) fh = CreateFile(sTempFileName, GENERIC_WRITE, 0, 0&, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0&) If WriteFile(fh, WriteIn, slen, wlen, 0&) Then
TempName = sTempFileName
Else
DeleteFile sTempFileName
TempName = "bad"
End If CloseHandle fhEnd Function我用以上函数创建一个临时文件,然后将 文本内容写入这个临时文件,如果写入成功,则返回 新创建的临时文件的名字 ,写入失败,删除 临时文件,返回 “bad” .但是,我发现写入到 临时文件里面的 文本内容都成了 乱码 ,这是什么原因???请那位指点一下!!
Dim sTempFileName As String
Dim slen As Long
Dim fh As Long
Dim wd As Boolean
Dim wlen As Long If IsEmpty(sPath) Then sPath = ".\WebNetEx\temp\"
If IsEmpty(sPrefix) Then sPrefix = "SPIDER"
lUnique = 0 'windows随机给名 sTempFileName = Space$(100)
GetTempFileName sPath, sPrefix, lUnique, sTempFileName
sTempFileName = Mid$(sTempFileName, 1, InStr(sTempFileName, Chr$(0)) - 1) '去掉多余空格 slen = LenB(WriteIn) fh = CreateFile(sTempFileName, GENERIC_WRITE, 0, 0&, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0&) If WriteFile(fh, WriteIn, slen, wlen, 0&) Then
TempName = sTempFileName
Else
DeleteFile sTempFileName
TempName = "bad"
End If CloseHandle fhEnd Function我用以上函数创建一个临时文件,然后将 文本内容写入这个临时文件,如果写入成功,则返回 新创建的临时文件的名字 ,写入失败,删除 临时文件,返回 “bad” .但是,我发现写入到 临时文件里面的 文本内容都成了 乱码 ,这是什么原因???请那位指点一下!!
Private Function mWriteFile(ByVal filename As String, ByVal writein As String) As Long
Dim hFile As Long
Dim i As Long
hFile = CreateFile(filename, GENERIC_WRITE, 0, 0&, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0&)
Debug.Print hFile
Dim buff() As Byte
buff = StrConv(writein, vbFromUnicode)
mWriteFile = WriteFile(hFile, buff(0), UBound(buff) + 1, i, ByVal 0&)
Erase buff
CloseHandle hFile
End FunctionPrivate Sub Command1_Click()
Dim mstr As String
mstr = "nihao 你好"
mWriteFile "e:\mcc.txt", mstrEnd Sub
Private Function mWriteFile(ByVal filename As String, ByVal writein As String) As Long
Dim hFile As Long
Dim slen As Long
Dim i As Long
hFile = CreateFile(filename, GENERIC_WRITE, 0, 0&, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0&)
slen = lstrlen(writein + Chr(0))
mWriteFile = WriteFile(hFile, ByVal writein, slen, i, ByVal 0&)
CloseHandle hFile
End FunctionPrivate Sub Command1_Click()
Dim mstr As String
mstr = "nihao 你好"
mWriteFile "e:\mcc.txt", mstrEnd Sublstrlen的声明如下:
Private Declare Function lstrlen Lib "kernel32" Alias "lstrlenA" (ByVal lpString As String) As Long