7,763
社区成员
Option Explicit
Sub test()
Dim FSO As New FileSystemObject, bFile As File
For Each bFile In FSO.GetFolder("f:\").Files
If bFile.Name Like "w?.txt" Then‘’这个文件名就是Alt+0160输入的
bFile.OpenAsTextStream(ForWriting).Write "sdvssnvs"
Debug.Print bFile.OpenAsTextStream(ForReading).ReadAll
End If
Next
End Sub
...Option Explicit
Private Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileW" (ByVal lpFileName As Long, lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileW" (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long
Private Declare Function WriteFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten As Long, ByVal lpOverlapped As Any) As Long
Private Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, ByVal lpOverlapped As Any) As Long
Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileW" (ByVal lpFileName As Long, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal lpSecurityAttributes As Any, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function GetFileSize Lib "kernel32" (ByVal hFile As Long, lpFileSizeHigh As Long) As Long
Const MAX_PATH = 260
Const MAXDWORD = &HFFFF
Const INVALID_HANDLE_VALUE = -1
Const FILE_SHARE_READ = &H1
Const FILE_SHARE_WRITE = &H2
Const CREATE_NEW = 1
Const OPEN_EXISTING = 3
Const GENERIC_READ = &H80000000
Const GENERIC_WRITE = &H40000000
Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Private Type WIN32_FIND_DATA
dwFileAttributes As Long
ftCreationTime As FILETIME
ftLastAccessTime As FILETIME
ftLastWriteTime As FILETIME
nFileSizeHigh As Long
nFileSizeLow As Long
dwReserved0 As Long
dwReserved1 As Long
cFileName(MAX_PATH * 2 - 1) As Byte 'Unicode 需要双倍长度
cAlternate(14 * 2 + 1) As Byte 'Unicode 需要双倍长度
End Type
Const eeEndOfFile = 62 'Input past end of file
Function StripNulls(bytes() As Byte) As String
Dim i As Long
For i = 0 To UBound(bytes) Step 2
If bytes(i) = 0 And bytes(i + 1) = 0 Then
Exit For
End If
Next
StripNulls = LeftB(bytes, i)
End Function
Function FindFiles(path As String, searchPattern As String) As Collection
Dim oFiles As Collection
Dim hSearch As Long
Dim bFound As Boolean
Dim sFileName As String
Dim WFD As WIN32_FIND_DATA
Set oFiles = New Collection
If Right(path, 1) <> "\" Then path = path & "\"
sFileName = path & searchPattern
hSearch = FindFirstFile(StrPtr(sFileName), WFD)
If hSearch <> INVALID_HANDLE_VALUE Then
bFound = True
While bFound
sFileName = StripNulls(WFD.cFileName)
If (sFileName <> ".") And (sFileName <> "..") Then
oFiles.Add path & sFileName
End If
bFound = FindNextFile(hSearch, WFD)
Wend
Call FindClose(hSearch)
End If
Set FindFiles = oFiles
End Function
Function ReadAllText(ByVal path As String) As String
Dim hFile As Long
Dim nSize As Long
Dim bytes() As Byte
Dim lBytesRead As Long
hFile = CreateFile(StrPtr(path), GENERIC_READ, FILE_SHARE_READ Or FILE_SHARE_WRITE, ByVal 0&, OPEN_EXISTING, 0, 0)
nSize = GetFileSize(hFile, 0)
ReDim bytes(nSize - 1) As Byte
ReadFile hFile, bytes(0), nSize, lBytesRead, ByVal 0&
CloseHandle hFile
If lBytesRead <> nSize Then Err.Raise eeEndOfFile
ReadAllText = StrConv(bytes, vbUnicode)
End Function
Sub Main()
Dim oFiles As Collection
Dim i As Long
Dim s As String
Set oFiles = FindFiles(App.path, "1*.*")
For i = 1 To oFiles.Count
s = oFiles(i)
Debug.Print s, "U+" & Hex(AscW(Mid$(s, InStrRev(s, "\") + 2, 1)))
Debug.Print ReadAllText(s)
Next
End Sub
D:\temp\UnicodeFileName\1?.TXT U+A0
abc中文
Imports System.Text
Imports System.IO
Module Module1
Sub Main()
For Each s As String In My.Computer.FileSystem.GetFiles("D:\temp\UnicodeFileName\", FileIO.SearchOption.SearchTopLevelOnly, "1*.*")
Console.WriteLine(s)
Console.WriteLine(BitConverter.ToString(Encoding.Unicode.GetBytes(Path.GetFileName(s))))
Console.WriteLine(My.Computer.FileSystem.ReadAllText(s, Encoding.Default))
Next
Console.ReadLine()
End Sub
End Module
Dim c(1) As Byte
c(0) = &HFF
c(1) = &HD8
Open bFile.Name For Binary As #1
Put #1, , c
Close #1