gpt4 book ai didi

unicode - 如何在 VB6 中读取/写入具有 Unicode 文件名的二进制文件

转载 作者:行者123 更新时间:2023-12-04 13:06:36 26 4
gpt4 key购买 nike

我有这个代码:

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

当我运行代码时,出现如图所示的错误消息:

enter image description here

错误在如图所示的行中:

enter image description here

如何让它支持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

关于unicode - 如何在 VB6 中读取/写入具有 Unicode 文件名的二进制文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69170075/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com