|
发表于 2012-4-18 22:53:40
|
显示全部楼层
本帖最后由 Jen 于 2012-4-18 22:58 编辑
我常用:- Public Function FileExists(FileName As String, Optional ByVal useUnicode As Boolean) As Boolean
- ' test to see if a file exists
- Const INVALID_HANDLE_VALUE = -1&
- Dim bISNT As Boolean
- bISNT = IsWinNt
- If bISNT = False Then bISNT = (Not (IsWindowUnicode(GetDesktopWindow) = 0&))
- If bISNT Then
- FileExists = Not (GetFileAttributesW(StrPtr(FileName)) = INVALID_HANDLE_VALUE)
- Else
- FileExists = Not (GetFileAttributesA(FileName) = INVALID_HANDLE_VALUE)
- End If
- End Function
- Private Function OpenFileW(FileName As String)
- ' Function uses APIs to read file name with unicode characters
- Const GENERIC_READ As Long = &H80000000
- Const GENERIC_WRITE = &H40000000
- Const OPEN_EXISTING = &H3
- Const FILE_SHARE_READ = &H1
- Const FILE_SHARE_WRITE = &H2
- Const FILE_ATTRIBUTE_ARCHIVE As Long = &H20
- Const FILE_ATTRIBUTE_HIDDEN As Long = &H2
- Const FILE_ATTRIBUTE_READONLY As Long = &H1
- Const FILE_ATTRIBUTE_SYSTEM As Long = &H4
- Const FILE_ATTRIBUTE_NORMAL = &H80&
- Dim Flags As Long, Access As Long
- Dim Disposition As Long, Share As Long
- Dim hFile As Long
- Access = GENERIC_READ
- Share = FILE_SHARE_READ
- Disposition = OPEN_EXISTING
- Flags = FILE_ATTRIBUTE_ARCHIVE Or FILE_ATTRIBUTE_HIDDEN Or FILE_ATTRIBUTE_NORMAL _
- Or FILE_ATTRIBUTE_READONLY Or FILE_ATTRIBUTE_SYSTEM
- hFile = CreateFileW(StrPtr(FileName), Access, Share, 0&, Disposition, Flags, 0&)
- If hFile = 0 Then
- If isStringANSI(FileName) Then
- ' hFile should never be zero. It should be -1 (error) or a valid handle
- ' when hFile is zero, most likely API was called on a Win9x system
- ' so we will call the ANSI version and see if that returns a handle
- hFile = CreateFileA(FileName, Access, Share, 0&, Disposition, Flags, 0&)
- End If
- End If
- If Not hFile = -1 Then OpenFileW = hFile
- End Function
- Public Function isStringANSI(inText As String) As Boolean
- ' simple test to determine if passed string is ANSI-like or not.
- ' In other words, does it contain unicode characters.
- Dim tArray() As Byte
- Dim x As Long
- If inText = vbNullString Then
- isStringANSI = True
- Else
- tArray = inText
- For x = LBound(tArray) + 1 To UBound(tArray) Step 2
- If Not tArray(x) = 0 Then Exit For
- Next x
- isStringANSI = (x > UBound(tArray))
- End If
- End Function
复制代码 |
|