如何在 VB6 中读取/写入具有 Unicode 文件名的二进制文件
How to read / write binary file with Unicode filename in VB6
我有这个代码:
Function cpyBIN(cpFilename As String)
Dim litefile() As Byte
Dim FN As Integer
Dim xlof As Long
FN = 1
Open cpFilename For Binary As FN
xlof = LOF(FN)
ReDim litefile(xlof)
Get FN, , litefile
Open cpFilename & "Backup" For
Binary As #2
Put #2, , litefile
Close #2
Close FN
End Function
我在这样的表格中使用它:
Private Sub cmdBackup_Click()
Dim strComputer, objWMIService, colFiles, objfile
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery("Select * from CIM_Datafile where Drive='D:' and path='\contoh\'")
For Each objfile In colFiles
cpyBIN (objfile.Name)
Next
End Sub
在 contoh 文件夹中有 2 个示例文件:
- namafile.exe
- namafile♣♣♠.exe
当我运行代码出现如图所示的错误信息:
错误在图中一行:
如何让它支持unicode文件名?
要么
这个功能有什么替代品吗??
复制具有 Unicode 文件名的文件有多种方法。第一种方法是使用 Windows API:
Declare Function CopyFileW Lib "kernel32.dll" (ByVal lpExistingFileName As Long, _
ByVal lpNewFileName As Long, Optional ByVal bFailIfExists As Long) As Long
For Each objfile In colFiles
CopyFileW StrPtr(objfile.Name), StrPtr(objfile.Name & ".Backup")
Next
第二种方法是使用 FileSystemObject:
Dim fso As FileSystemObject
Set fso = New FileSystemObject
For Each objfile In colFiles
fso.CopyFile objfile.Name, objfile.Name & ".Backup", True
Next
第三种方法是使用 ADO 流:
Dim s As ADODB.Stream
Set s = New ADODB.Stream
s.Open
s.Type = adTypeBinary
For Each objFile In colFiles
s.LoadFromFile objFile.Name
s.SaveToFile objFile.Name & ".Backup", adSaveCreateOverWrite
Next
s.Close
如果你想读取数据,我会使用 ADO 流:
Dim s As ADODB.Stream
Set s = New ADODB.Stream
s.Open
s.Type = adTypeBinary
For Each objFile In colFiles
s.LoadFromFile objFile.Name
data = s.Read()
'use the data somehow
Next
s.Close
我有这个代码:
Function cpyBIN(cpFilename As String)
Dim litefile() As Byte
Dim FN As Integer
Dim xlof As Long
FN = 1
Open cpFilename For Binary As FN
xlof = LOF(FN)
ReDim litefile(xlof)
Get FN, , litefile
Open cpFilename & "Backup" For
Binary As #2
Put #2, , litefile
Close #2
Close FN
End Function
我在这样的表格中使用它:
Private Sub cmdBackup_Click()
Dim strComputer, objWMIService, colFiles, objfile
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery("Select * from CIM_Datafile where Drive='D:' and path='\contoh\'")
For Each objfile In colFiles
cpyBIN (objfile.Name)
Next
End Sub
在 contoh 文件夹中有 2 个示例文件:
- namafile.exe
- namafile♣♣♠.exe
当我运行代码出现如图所示的错误信息:
错误在图中一行:
如何让它支持unicode文件名? 要么 这个功能有什么替代品吗??
复制具有 Unicode 文件名的文件有多种方法。第一种方法是使用 Windows API:
Declare Function CopyFileW Lib "kernel32.dll" (ByVal lpExistingFileName As Long, _
ByVal lpNewFileName As Long, Optional ByVal bFailIfExists As Long) As Long
For Each objfile In colFiles
CopyFileW StrPtr(objfile.Name), StrPtr(objfile.Name & ".Backup")
Next
第二种方法是使用 FileSystemObject:
Dim fso As FileSystemObject
Set fso = New FileSystemObject
For Each objfile In colFiles
fso.CopyFile objfile.Name, objfile.Name & ".Backup", True
Next
第三种方法是使用 ADO 流:
Dim s As ADODB.Stream
Set s = New ADODB.Stream
s.Open
s.Type = adTypeBinary
For Each objFile In colFiles
s.LoadFromFile objFile.Name
s.SaveToFile objFile.Name & ".Backup", adSaveCreateOverWrite
Next
s.Close
如果你想读取数据,我会使用 ADO 流:
Dim s As ADODB.Stream
Set s = New ADODB.Stream
s.Open
s.Type = adTypeBinary
For Each objFile In colFiles
s.LoadFromFile objFile.Name
data = s.Read()
'use the data somehow
Next
s.Close